带有eval代码执行的heredoc

时间:2009-08-12 08:44:51

标签: php eval heredoc

我尝试了几种尝试让它工作但没有运气的方法!

我有一个这样的页面(例子):

<?php
$jj = <<<END
?>
<h1>blah blah</h1>
<p> blah blah blah blah blah blah blah <?php include("file.php"); ?> blah blah</p>
<?php
END;
eval('?>'.$jj.'<?php ');
?>

这导致没有输出如此,无法想到解决方案!

1 个答案:

答案 0 :(得分:3)

这不起作用,因为eval只需要PHP代码(即不包含&lt;?php?&gt;标记),因此对eval()的调用可能会因解析错误而失败。

我建议使用输出缓冲,例如:

<?php
//start output buffering, anything outputted should be stored in a buffer rather than being sent to the browser
ob_start();
?>

<h1>blah blah</h1>
<p> blah blah blah blah blah blah blah <?php include("file.php"); ?> blah blah</p>

<?php
//get contents of buffer and stop buffering
$content = ob_get_clean();
echo $content;
?>