【问题标题】:Assigning an Include to a string variable PHP将 Include 分配给字符串变量 PHP
【发布时间】:2013-04-18 19:21:57
【问题描述】:

我有需要进一步处理的模板文件,因此想使用包含而不使用 file_get_contents 将其分配给字符串变量。这样做的原因是因为最终输出是通过ob缓冲的,并且包含需要执行的php。如果我使用 file_get_contents 它不会执行。问题是如果我使用 $output = include 'file.php' 它会打印输出而不分配。

【问题讨论】:

  • 输出缓冲在哪里 - 包含的脚本或主脚本?
  • 再增加一层输出缓冲,使用ob_get_clean()
  • 主脚本调用一个类来设置页面和处理模板。在主页中它调用这个类,处理输出,我使用 ob_start() //$template = ob_get_contents(); //ob_clean(); //$registry->getObject('template')->parseOutput(); //$template = $registry->getObject('template')->getPage()->getContentToPrint(); //$template = str_replace(array("\r\n","\n","\r"),'',$template); //回显$模板; //ob_end_flush();
  • $template = ob_get_contents(); ob_clean(); $registry->getObject('template')->parseOutput(); $template = $registry->getObject('template')->getPage()->getContentToPrint(); $template = str_replace(array("\r\n","\n","\r"),'',$template);回声$模板; ob_end_flush();抱歉,我已经评论了测试行
  • 我相信CORRUPT下面的答案正是你想要的,但请注意,如果你在file_get_contents()中使用url而不是文件路径,php应该执行。

标签: php include


【解决方案1】:

试试这个:

ob_start();

include 'file.php';

$output = ob_get_clean();

【讨论】:

  • 这是我决定在课堂上进行处理之前使用的方法。我决定上这门课的原因是为了更好地组织、处理和存储模板。但是我不知道使用 URL 而不是 file_get_contents 的路径会允许 php 执行。感谢您的提示。
猜你喜欢
  • 1970-01-01
  • 2019-02-08
  • 2013-06-08
  • 2013-02-23
  • 1970-01-01
  • 1970-01-01
  • 2021-05-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多