如何将PHP包含定义为字符串? [英] How do you define a PHP include as a string?

查看:165
本文介绍了如何将PHP包含定义为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过了:

$test = include 'test.php';

但通常只包含文件

推荐答案

您需要查看输出缓冲函数。

You'll want to look at the output buffering functions.

//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指出的那样,输出缓冲区堆栈。所以理论上你可以这样做:

As Jeremy points out, output buffers stack. So you could theoretically just do something like:

<?PHP
function return_output($file){
    ob_start();
    include $file;
    return ob_get_clean();
}
$content = return_output('some/file.php');

这应该等同于我更详细的原始解决方案。

This should be equivalent to my more verbose original solution.

但我没有费心去测试这个。

But I haven't bothered to test this one.

这篇关于如何将PHP包含定义为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆