将Perl输出返回到PHP [英] Return Perl-output to PHP

查看:58
本文介绍了将Perl输出返回到PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将perl脚本的输出返回到网页.但是,它仅返回最后一行.

I want to return the output of a perl script to a webpage. However it only returns the last line.

Perl脚本:

my $directory = $ARGV[0];
opendir(DIR,$directory);
my @files = grep {/\.txt$/ } readdir(DIR);
closedir(DIR);
foreach(@files) {
    print $_."\n";
}

PHP代码:

$perl_result = exec("perl $script_folder $project_folder");
*some code*
<?php print $perl_result; ?>

预期的输出(以及脚本在Linux命令行中返回的内容):

Expected output (and what the script returns in a Linux command line):

test.txt
test2.txt
test3.txt

PHP返回的内容:

test3.txt

为了使PHP显示所有行,我必须在代码中进行哪些更改?

What do I have to change in my code to get PHP to show all lines?

谢谢

推荐答案

PHP引用exec() 的手册页:

Quoting from the PHP manual page for exec():

返回值

命令结果的最后一行.如果您需要执行命令并直接将命令中的所有数据传回而不会受到任何干扰,请使用 passthru() 函数.

The last line from the result of the command. If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru() function.

要获取已执行命令的输出,请确保设置并使用output参数.

To get the output of the executed command, be sure to set and use the output parameter.

因此,一个建议是停止使用exec()并开始使用 passthru() .但是,那是胡说八道. passthru()实际上不返回任何内容.如果您需要对$perl_result做的所有 all 打印到浏览器中就足够了,因此根本不需要将输出存储在变量中.但是,如果您需要与输出进行匹配或以任何方式对其进行操作,则不需要passthru().

So one suggestion is stop using exec() and start using passthru() . However, that's nonsense. passthru() doesn't actually return anything. It may be sufficient if all you need to do with $perl_result is print it to the browser, and thus don't really need the output to be stored in a variable at all. But if you need to match against the output, or manipulate it in any way, you don't want passthru().

相反,请尝试反引号操作符:

<?php
$perl_result = `perl $script_folder $project_folder`;

或尝试将exec()的第二个参数设置为空数组:

Or try setting the second argument of exec() to an empty array:

<?php
$perl_result = array();
exec("perl $script_folder $project_folder", $perl_result);

$perl_result = implode("\n", $perl_result);  # array --> string

这篇关于将Perl输出返回到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆