我想要做这样的事情:读线从STDIN
$ [mysql query that produces many lines] | php parse_STDIN.php
在parse_STDIN.php
文件我希望能够通过线从标准输入解析我的数据线。
我想要做这样的事情:读线从STDIN
$ [mysql query that produces many lines] | php parse_STDIN.php
在parse_STDIN.php
文件我希望能够通过线从标准输入解析我的数据线。
你也可以使用一个发生器 - 如果你不知道STDIN将会有多大。
需要PHP 5> = 5.5.0,PHP线沿线的7
东西:
function stdin_stream()
{
while ($line = fgets(STDIN)) {
yield $line;
}
}
foreach (stdin_stream() as $line) {
// do something with the contents coming in from STDIN
}
您可以在此处详细了解发生器(或谷歌搜索教程): http://php.net/manual/en/language.generators.overview.php
如果STDIN中的某行将为空或“0”,该怎么办? – luchaninov 2016-09-27 11:30:36
由于'fgets()'将换行符保留在最后(“\ n”),所以直到EOF才会发生。 – MatsLindh 2017-07-26 13:48:36
您是否尝试过从PHP脚本中调用该查询?你知道mysqli_query(“SELECT'many_lines' FROM'big_table'”);?这样,你不必传递任何参数或任何东西。 PHP是用于Web服务器的脚本语言。如果您想要真正的处理解决方案,请使用编程语言,如C. – ATaylor 2012-08-15 11:11:27
首先:为什么?为什么不直接在PHP脚本中运行查询?其次:'while(!feof(STDIN)){$ line = fgets(STDIN); do_stuff($线); }' – DaveRandom 2012-08-15 11:13:00
您应该了解PHP手册中的* Commandline Usage *部分:[Input/output streams](http://php.net/manual/features.commandline.io-streams.php) – hakre 2012-08-15 11:22:46