是否可以更改 PHP 的 print_r 函数的行为 [英] Is it possible to change the behavior of PHP's print_r function

查看:29
本文介绍了是否可以更改 PHP 的 print_r 函数的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用 PHP 编码了很长时间(现在 15 年以上),我通常在 Windows 操作系统上这样做,尽管大部分时间是在 Linux 服务器上执行.多年来,我遇到了一个烦恼,虽然它并不重要,但事实证明它有点令人恼火,我已经到了想看看我是否可以以某种方式解决它的地步.问题来了:

I've been coding in PHP for a long time (15+ years now), and I usually do so on a Windows OS, though most of the time it's for execution on Linux servers. Over the years I've run up against an annoyance that, while not important, has proved to be a bit irritating, and I've gotten to the point where I want to see if I can address it somehow. Here's the problem:

在编码时,我经常发现将数组的内容输出到文本文件很有用,以便我可以查看其内容.例如:

When coding, I often find it useful to output the contents of an array to a text file so that I can view it's contents. For example:

$fileArray = file('path/to/file');
$faString = print_r($fileArray, true);
$save = file_put_contents('fileArray.txt', $faString);

现在,当我在记事本中打开文件 fileArray.txt 时,文件的内容都显示在一行中,而不是在写字板中打开文件时看到的漂亮、漂亮的结构.这是因为,无论操作系统如何,PHP 的 print_r 函数都使用 \n 作为换行符,而不是 \r\n.我当然可以通过简单地添加一行代码来进行必要的替换来自己执行这样的替换,这就是问题所在.那一行,一行额外的代码在我的岁月中转化为数百个不必要的额外步骤.我是一个懒惰的程序员,这已经变得不可接受了.

Now when I open the file fileArray.txt in Notepad, the contents of the file are all displayed on a single line, rather than the nice, pretty structure seen if the file were opened in Wordpad. This is because, regardless of OS, PHP's print_r function uses \n for newlines, rather than \r\n. I can certainly perform such replacement myself by simply adding just one line of code to make the necessary replacements, ans therein lies the problem. That one, single line of extra code translates back through my years into literally hundreds of extra steps that should not be necessary. I'm a lazy coder, and this has become unacceptable.

目前,在我的开发机器上,我有一种不同的解决方法(如下所示),但这有它自己的一系列问题,所以我想找到一种强制"的方法PHP 在没有所有额外代码的情况下放入正确的"换行符.我怀疑这可能是可能的,但如果我不问,我永远不会知道,所以...

Currently, on my dev machine, I've got a different sort of work-around in place (shown below), but this has it's own set of problems, so I'd like to find a way to "coerce" PHP into putting in the "proper" newline characters without all that extra code. I doubt that this is likely to be possible, but I'll never find out if I never ask, so...

无论如何,我目前的解决方法是这样的.我的 PHP 包含路径中有一个文件 (print_w.php),其中包含以下代码:

Anyway, my current work-around goes like this. I have, in my PHP include path, a file (print_w.php) which includes the following code:

<?php

  function print_w($in, $saveToString = false) {
    $out = print_r($in, true);
    $out = str_replace("\n", "\r\n", $out);
    switch ($saveToString) {
      case true: return $out;
      default: echo $out;
    }
  }
?>

我还在 php.ini 中将 auto_prepend_file 设置为同一个文件,这样每次 PHP 在我的开发机器上执行脚本时它都会自动包含它.然后我在测试我的脚本时使用函数 print_w 而不是 print_r.这很有效,只要当我将脚本上传到远程服务器时,我确保删除或注释掉对函数 print_w 的所有引用.如果我错过了一个,我(当然)会得到一个致命的错误,这可能比原来的问题更令人沮丧,但我强调在上传之前仔细校对我的代码,所以这通常不是问题.

I also have auto_prepend_file set to this same file in php.ini, so that it automatically includes it every time PHP executes a script on my dev machine. I then use the function print_w instead of print_r while testing my scripts. This works well, so long as when I upload a script to a remote server I make sure that all references to the function print_w are removed or commented out. If I miss one, I (of course) get a fatal error, which can prove more frustrating than the original problem, but I make it a point to carefully proofread my code prior to uploading, so it's not often an issue.

所以,在所有这些漫无边际之后,我的问题是,有没有办法改变 print_r(或类似的 PHP 函数)的行为以使用 Windows 换行符,而不是 Windows 机器上的 Linux 换行符?

So after all that rambling, my question is, Is there a way to change the behavior of print_r (or similar PHP functions) to use Windows newlines, rather than Linux newlines on a Windows machine?

感谢您的时间.

推荐答案

好的,经过进一步研究,我找到了一个更好的解决方法来满足我的需求,并且不需要调用自定义函数而不是 print_r.这种新的解决方法是这样的:

Ok, after further research, I've found a better work-around that suite my needs, and eliminates the need to call a custom function instead of print_r. This new work-around goes like this:

我仍然必须有一个包含文件(我保留了相同的名称,以免与 php.ini 混淆),并且 php.ini 仍然具有 auto_prepend_file 设置,但是 print_w 中的代码.php 有点变化:

I still have to have an included file (I've kept the same name so as not to have to mess with php.ini), and php.ini still has the auto_prepend_file setting in place, but the code in print_w.php is changes a bit:

<?php

  rename_function('print_r', 'print_rw');

  function print_r($in, $saveToString = false) {
    $out = print_rw($in, true);
    $out = str_replace("\n", "\r\n", $out);
    switch ($saveToString) {
      case true: return $out;
      default: echo $out;
    }
  }

?>

这有效地改变了我本地机器上的 print_r 函数的行为,我不必调用自定义函数,也不必确保对该自定义函数的所有引用都被中和.通过使用 PHP 的 rename_function 我能够有效地重写 print_r 的行为方式,使可以解决我的问题.

This effectively alters the behavior of the print_r function on my local machine, without my having to call custom functions, and having to make sure that all references to that custom function are neutralized. By using PHP's rename_function I was able to effectively rewrite how print_r behaves, making it possible to address my problem.

这篇关于是否可以更改 PHP 的 print_r 函数的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆