开发者

How to get output from local script in PHP?

For example, in foo.php:

<?php echo 'hello'; ?>

And in bar.php, I want to get开发者_开发百科 the output of foo.php (which is hello) and do some formatting before outputting to browser. Is there any way to do this?

Or even further, if the webserver can run both PHP and Python scripts, can a PHP script get the output of a Python script?

Edit: PHP function file_get_contents() can do this only for remote scripts. If it is used on local scripts, it will return the contents of the whole script. In the example above, it returns rather than hello. And I don't want to use exec()/system() things and CGI.


You can use PHP's output buffers and functions like ob_start(); and ob_get_contents(); to read the contents of your PHP output into a string.

Here is the example on php.net:

<?php

function callback($buffer)
{
  // replace all the apples with oranges
  return (str_replace("apples", "oranges", $buffer));
}

ob_start("callback");

?>
<html>
<body>
<p>It's like comparing apples to oranges.</p>
</body>
</html>
<?php

ob_end_flush();

?>

And another:

<?php
ob_start();
echo "Hello ";
$out1 = ob_get_contents();
echo "World";
$out2 = ob_get_contents();
ob_end_clean();
var_dump($out1, $out2);
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜