如何使用ob_start在PHP中的另一个脚本中输出和处理脚本?
问题描述:
OK的样子:如何使用ob_start在PHP中的另一个脚本中输出和处理脚本?
我想要做的是以下(这是一个例子):
- $输出=要求( “script_execution.php”);
- echo str_replace(“hello”,“bye”,$ output);
的溶液I具有:
(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”有(退出;)会发生什么? 因为在第三条指令停止所有执行运行之前,最终输出将会出错。
í如何在不退出我的脚本(solution.php)的情况下获得“script_execution.php”的最终输出?因为你知道,script_execution.php的最终输出(独立的出口;指令)是:
的hello world ....
谢谢! (可能使用线程)
答
如果您使用include
或require
评估脚本,并且它调用exit()
,那么脚本将终止。
你有两个选择,我看到:
使用shutdown处理与
register_shutdown_function()
当exit()
被调用运行,然后捕获功能中的输出缓冲器和打印。-
通过使一个HTTP请求到服务器只执行
script_execution.php
,类似的东西:$output = file_get_contents('http://www.yoursite.com/script_execution.php');
太棒了!我将使用第一个用于ob_start ..也许第二个用于另一个用途。谢谢! –