PHP:将多行命令行输出输出为不同的行

PHP:将多行命令行输出输出为不同的行

问题描述:

PHP: Output multiple line command-line outputs as different lines. Sorry if the title is difficult to understand. Basically I want my output like A, instead of B. It currently looks like B. I have tried nl2br. The script I am trying to run is:

Script:

echo "Virus Scan Results:";
$scanme = system('cd /var/www/upload/files; clamscan --remove=yes '.$furl);
printf(nl2br($scanme));

A:

802931t_e_s_t.txt: OK 
----------- SCAN SUMMARY ----------- 
Known viruses: 574585 
Engine version: 0.95.1 
Scanned directories: 0 
Scanned files: 1 
Infected files: 0 
Data scanned: 0.00 MB 
Data read: 0.00 MB (ratio 0.00:1) 
Time: 2.352 sec (0 m 2 s) 
Time: 2.352 sec (0 m 2 s)

B:

802931t_e_s_t.txt: OK ----------- SCAN SUMMARY ----------- Known viruses: 574585 Engine version: 0.95.1 Scanned directories: 0 Scanned files: 1 Infected files: 0 Data scanned: 0.00 MB Data read: 0.00 MB (ratio 0.00:1) Time: 2.352 sec (0 m 2 s) Time: 2.352 sec (0 m 2 s)

PHP:将多行命令行输出输出为不同的行。 如果是 标题很难理解。 基本上我希望我的输出像A,而不是B.它目前看起来像B.我试过nl2br。 我试图运行的脚本是: p>

脚本: p>

 
echo“病毒扫描结果:”; 
 $ scanme = system('  cd / var / www / upload / files; clamscan --remove = yes'。$ furl); 
printf(nl2br($ scanme)); 
  pre> 
 
 

A: p >

 
802931t_e_s_t.txt:OK 
 -----------扫描摘要----------- 
未知病毒:574585 
发动机版本:0.95  .1 
扫描目录:0 
扫描文件:1 
感染文件:0 
扫描数据:0.00 MB 
读取数据:0.00 MB(比例0.00:1)
时间:2.352秒(0 m 2 s)
时间:2.352 秒(0 m 2 s)
  pre> 
 
 

B: p>

 
802931t_e_s_t.txt:OK ----------- 扫描摘要-----------已知病毒:574585引擎版本:0.95.1扫描目录:0扫描文件:1感染文件:0扫描数据:0.00 MB读取数据:0.00 MB(比例0.00:1  )时间:2.352秒(0 m 2 s)时间:2.352秒(0 m 2 s)
  pre> 
 
 

p> div>

why are you using nl2br if this is on the command line?

nl2br outputs <br /> tags for new lines... which would have no meaning on command line

Edit

Two things:

1 try

system('cd /var/www/upload/files; clamscan --remove=yes '.$furl, $scanme);

2 You may want to use the exec function instead of system

e.g.

exec('cd /var/www/upload/files; clamscan --remove=yes '.$furl, $scanme);
$scanme = implode("
",$scanme);

exec ( string $command [, array &$output [, int &$return_var ]] )

If you are running on the commandline, a newline is represented as ' ', or ' ' in a Windows environment. So make sure there is a at the end of each line, and you should get the output you want. Edit:
Tom: Oops. Fixed.

Have you tried just printing the output of the command directly?

echo "Virus Scan Results:";
echo exec('cd /var/www/upload/files; clamscan --remove=yes '.$furl);

PS. You really should sanitise the input like so (if you are not doing so already):

$furl = escapeshellarg($furl)

escapeshellarg() - Escape a string to be used as a shell argument