如何在生成模板时评估PHP-获取语法错误



我有一个html文件,其中的语法像"@echosomething",然后我想用file_get_contents加载该html文件,并替换template关键字,这样它就会回显一个变量,但我遇到了语法错误。

Parse error: syntax error, unexpected '<', expecting end of file in C:xampphtdocstemplatingindex.php(9) : eval()'d code on line 1

这是我的html_file.html

<html>
<body>
@echovalue
</body>

这是我的php代码

<?php
$html_file = file_get_contents( 'html_file.html' );
$value = "This is a test value!";
$str = '<?php echo $value; ?>';
$html_file = str_replace('@echovalue', $str, $html_file);
eval($html_file);
echo $html_file;
?>

为什么不简单地

$html_file = str_replace('@echovalue', $value, $html_file);

$html_file = file_get_contents( 'html_file.html' );
$value = "This is a test value!";
$str = 'return $value;';
echo str_replace('@echovalue', eval($str), $html_file);
echo $html_file;

最新更新