为什么php'sshell_exec命令会在输出中添加不需要的换行符 (\r\n),我该如何防止这种情况发生?
测试.php:
<?php
var_dump(shell_exec('echo "test"'));
运行php test.php结果:
string(5) "test
"
为什么php'sshell_exec命令会在输出中添加不需要的换行符 (\r\n),我该如何防止这种情况发生?
测试.php:
<?php
var_dump(shell_exec('echo "test"'));
运行php test.php结果:
string(5) "test
"
该echo命令添加换行符,以便您的示例按预期工作。如果你想删除它,只需使用trim:
var_dump(trim(shell_exec('echo "test"')));
这将输出:
string(5) "test"
您可以将-n作为参数传递给您的echo命令,这将防止echo输出尾随换行符。
从手册:
-n 不输出尾随换行符