看来你需要使用
Output Buffering Control – 特别是
ob_start()和
ob_get_clean()功能.
使用输出缓冲将允许您将标准输出重定向到内存,而不是将其发送到浏览器.
这是一个简单的例子:
// Activate output buffering => all that's echoed after goes to memory
ob_start();
// do some echoing -- that will go to the buffer
echo "hello %MARKER% !!!";
// get what was echoed to memory, and disables output buffering
$str = ob_get_clean();
// $str now contains what whas previously echoed
// you can work on $str
$new_str = str_replace('%MARKER%', 'World', $str);
// echo to the standard output (browser)
echo $new_str;
你得到的输出是:
hello World !!!