你会想看看输出缓冲功能。
//get anything that's in the output buffer, and empty the buffer
$oldContent = ob_get_clean();
//start buffering again
ob_start();
//include file, capturing output into the output buffer
include "test.php";
//get current output buffer (output from test.php)
$myContent = ob_get_clean();
//start output buffering again.
ob_start();
//put the old contents of the output buffer back
echo $oldContent;
编辑:
Jeremy指出,输出缓冲区堆栈。所以你理论上只能做一些事情:
function return_output($file){
ob_start();
include $file;
return ob_get_clean();
}
$content = return_output('some/file.php');
这应该相当于我更详细的原始解决方案。
但是我没有打扰测试这个。