在PHP中正确打印Python脚本的输出

2024-05-23 23:04:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个php文件调用一个脚本并输出如下


    $output=shell_exec('/usr/bin/python hello.py');
    echo $output;

它打印出来


    b'total 16\ndrwx---r-x 2 oae users 4096 Jul 31 14:21 .\ndrwxr-x--x+ 9 oae root 4096 Jul 26 13:59 ..\n-rwx---r-x 1 oae users 90 Aug 3 11:22 hello.py\n-rwx---r-x 1 oae users 225 Aug 3 11:22 index.php\n'

但应该是这样的


    total 16K
    drwx---r-x  2 oae users 4.0K Jul 31 14:21 ./
    drwxr-x--x+ 9 oae root  4.0K Jul 26 13:59 ../
    -rwx---r-x  1 oae users   90 Aug  3 11:22 hello.py*
    -rwx---r-x  1 oae users  225 Aug  3 11:22 index.php*

\n不应显示字符。如何解决此问题?


Tags: 文件py脚本hellooutputindexrootshell
3条回答

这是可行的

$output=shell_exec('/usr/bin/python hello.py');
echo "<pre>";
print_r($output);
echo "</pre>";

希望你在网上打印这个字符串。 在这种情况下,PHP可能不使用字符,而应该是<br/> PHP有一个内置函数来处理字符串的这种转换。

http://php.net/manual/en/function.nl2br.php

另一种方法是在<pre>...</pre>标记之间包装字符串。

相关问题 更多 >