我仍然是PHP的新手,所以请原谅我,如果我在这里遗漏了一些明显的东西。
所以,我正在调用一些产生文本行的后端脚本,我想我会修改exec
以确保它正常工作。当然,在(Linux)shell中,一个简单的多行输出命令类似于
echo a;echo b;echo c
当然会产生人们的期望:
a
b
c
那么,为什么我在下面exec
运行时会得到额外的“c”?
输入:
exec("echo a;echo b;echo c",$output,$return);
echo("return: $return\n");
echo("count: ". count($output) . "\n");
foreach($output as $i)
{print "$i\n";}
var_dump($output);
输出:
return: 0
count: 4
a
b
c
c
array(4) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
[3]=>
string(1) "c"
}
现在,我知道exec
返回已处理的最后一行,但我没有回显exec
的返回值。更令人费解的是,如果我让字符串更“随机”,它似乎工作正常:
jmaney> php -r 'exec("echo asfd;echo asdfasf;echo grrrr",$output,$return);echo("return: $return\n");echo("count: ". count($output) . "\n");foreach($output as $i){print "$i\n";}var_dump($output);'
return: 0
count: 3
asfd
asdfasf
grrrr
array(3) {
[0]=>
string(4) "asfd"
[1]=>
string(7) "asdfasf"
[2]=>
string(5) "grrrr"
}
我在这里缺少什么?
编辑添加:我正在运行bash shell版本3.2.48,SUSE Enterprise Linux版本11,Linux内核版本2.6和PHP版本5.2.12。
答案 0 :(得分:5)
实际上,是一个在php 5.2.13中修补过的bug(Bug#50732 exec()将单字节两次添加到$ output array https://bugs.php.net/bug.php?id=50732)