如何使用ob_start在PHP中的另一个脚本中获得脚本输出和处理


How to get scripts output and process in another script in PHP using ob_start?

OK外观:

我想做的是以下(这是一个例子):

  1. $output=require("script_execution.php")
  2. echo str_replace("你好"、"再见"、$output)

我的解决方案:

(script_execution.php)

<?php
   echo "hello world....";
?>

(solution.php)

<?php
  ob_start();
  require( "script_execution.php" );
  $output = ob_get_contents();
  ob_end_clean();
  echo $output; // WOW!!! but.......................
?>

(输出)

再见世界

问题是:该解决方案运行良好,但是,如果"script_execution.php"有(exit;)会发生什么???最终输出将是错误的,因为在第三条指令之前,所有正在运行的执行都将停止。

?在不退出脚本(solution.php)的情况下,我可以做些什么来获得"script_execution.php"的最终输出?因为正如您所知,script_execution.php(独立于出口;指令)的最终输出是:

你好世界

谢谢!(可能使用线程?)

如果使用includerequire评估脚本,并且它调用exit(),则脚本将终止。

我看到你有两个选择:

  1. 当调用exit()时,使用带有register_shutdown_function()的关闭处理程序运行,然后捕获函数中的输出缓冲区并打印它

  2. 通过向服务器发出HTTP请求,只执行script_execution.php,类似于:

    $output = file_get_contents( 'http://www.yoursite.com/script_execution.php');