Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Include赋值给字符串变量PHP_Php_Include - Fatal编程技术网

将Include赋值给字符串变量PHP

将Include赋值给字符串变量PHP,php,include,Php,Include,我有需要进一步处理的模板文件,所以希望使用include将其分配给字符串变量,而不使用file\u get\u内容。这是因为最终输出通过ob进行缓冲,并包含需要执行的php。如果我使用文件获取内容,它将不会执行。问题是,如果我使用$output=include'file.php',它将打印输出而不指定 试试这个: ob_start(); include 'file.php'; $output = ob_get_clean(); 输出缓冲在哪里?包括的脚本还是主脚本?添加另一层输出缓冲,并使

我有需要进一步处理的模板文件,所以希望使用include将其分配给字符串变量,而不使用file\u get\u内容。这是因为最终输出通过ob进行缓冲,并包含需要执行的php。如果我使用文件获取内容,它将不会执行。问题是,如果我使用$output=include'file.php',它将打印输出而不指定

试试这个:

ob_start();

include 'file.php';

$output = ob_get_clean();

输出缓冲在哪里?包括的脚本还是主脚本?添加另一层输出缓冲,并使用
ob\u get\u clean()
主脚本调用一个类来设置页面和处理模板。在主页面中,它调用这个类,处理输出,我使用ob_start()/$template=ob_get_contents()//ob_clean()//$注册表->获取对象('template')->parseOutput()//$template=$registry->getObject('template')->getPage()->GetContentTopPrint()//$template=str_replace(数组(“\r\n”、“\n”、“\r”)、“$template”)//echo$模板//ob_end_flush()$模板=ob_get_contents();ob_clean()$注册表->获取对象('template')->parseOutput()$template=$registry->getObject('template')->getPage()->GetContentTopPrint()$template=str_replace(数组(“\r\n”、“\n”、“\r”)、“$template”);echo$模板;ob_end_flush();很抱歉,我的测试中有行注释。我相信下面的答案是
CORRUPT
,这正是您想要的,但请注意,如果您在
file\u get\u contents()
中使用url而不是文件路径,php应该执行。这是我决定在类中进行处理之前使用的方法。我决定上这个课的原因是为了更好地组织、处理和存储模板。然而,我不知道使用URL而不是文件内容的路径将允许php执行。谢谢你的提示。