【问题标题】:PHP writing lots of text to STDERRPHP 向 STDERR 写入大量文本
【发布时间】:2012-08-20 14:30:07
【问题描述】:

在 PHP 中向STDOUT 写入大块时,您可以这样做:

echo <<<END_OF_STUFF
lots and lots of text
over multiple lines
etc.etc
END_OF_STUFF;

(即heredoc

我需要做类似的事情,但要STDERR。是否有另一个类似echo 的命令,但使用STDERR 代替?

【问题讨论】:

  • 你想究竟做什么?你试过做什么?
  • 也许这篇文章会对你有所帮助 - stackoverflow.com/a/554775/558021。写信给php://stderr
  • 我希望得到echo 的道德等价物,但改为使用STDERR
  • 回声就是回声——这一切都取决于您放置它的位置。您可以编写自己的自定义 "error_echo()" 函数...

标签: php echo stdout stderr


【解决方案1】:

是的,使用 php:// 流包装器:​​http://php.net/manual/en/wrappers.php.php

$stuff = <<<END_OF_STUFF
lots and lots of text
over multiple lines
etc.etc
END_OF_STUFF;

$fh = fopen('php://stderr','a'); //both (a)ppending, and (w)riting will work
fwrite($fh,$stuff);
fclose($fh);

【讨论】:

【解决方案2】:

对于一个简单的解决方案 - 试试这个

file_put_contents('php://stderr', 'This text goes to STDERR',FILE_APPEND);

FILE_APPEND 参数将追加数据而不是覆盖它。 您还可以使用fopenfwrite 函数直接写入错误流。

更多信息请访问 -http://php.net/manual/en/features.commandline.io-streams.php

【讨论】:

  • 一个小评论:像这样添加LOCK_EX标志很好:FILE_APPEND | LOCK_EX。
【解决方案3】:

CLI SAPI 中,它可以像使用STDERR 常量将Heredoc 字符串作为参数传递给fwrite() 一样简单。

fwrite(STDERR, <<< EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD
);

【讨论】:

    猜你喜欢
    • 2012-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-27
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    • 2016-04-21
    相关资源
    最近更新 更多