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 welcome) and do some formatting before exiting to the browser. Is there any way to do this?

Or even further, if the web server can run PHP and Python scripts, can a PHP script get Python script output?

Edit: The PHP function file_get_contents () can only do this for remote scripts. If used in local scripts, it will return the contents of the entire script. In the above example, it returns instead of a greeting. And I do not want to use the exec () / system () and CGI functions.

+3
source share
1 answer

PHP, ob_start(); ob_get_contents();, PHP .

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 like comparing apples to oranges.</p>
</body>
</html>
<?php

ob_end_flush();

?>

:

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

Source: https://habr.com/ru/post/1735687/


All Articles