【发布时间】:2011-01-18 12:17:53
【问题描述】:
我有一个 PHP + HTML 代码作为包含函数的字符串。我希望将一些函数调用替换为包含的内容。将多个文件合二为一。
简短示例 - 之前
<html>
<?php myclass->my_function('one', 'two', 'three'); ?>
<h1>Content in between</h1>
<?php myclass->my_function('1', '2'); ?>
</html>
简短示例 - 替换内容
<html>
<?php run('one', 'two', 'three'); /* Run does include a file */ ?>
<h1>Content in between</h1>
<?php run('1', '2'); /* Run does include a file */ ?>
</html>`
简短示例 - 之后
<html>
<?php
/* Start my_function */
echo 'This is the result of my_function one two three';
/* End my_function
?>
<h1>Content in between</h1>
<?php
/* Start my_function */
<?php myclass->my_function('four', 'five', 'six'); ?>
/* End my_function
?>
</html>
请注意,myclass 在包含的内容中。结果需要再次解析。
简短示例 - 之后
再次解析整个 this 并将 myclass 替换为包含的内容。
<html>
<?php
/* Start my_function */
echo 'This is the result of my_function one two three';
/* End my_function */
?>
<h1>Content in between</h1>
<?php
/* Start my_function */
echo 'four five six';
/* End my_function */
?>
</html>
我尝试使用爆炸来做到这一点,但它变得复杂了。 “替换内容”和“之后”这两个步骤可能需要一次性完成。
将其视为一个字符串。 preg_replace 可以解决这个问题吗?怎么样?
【问题讨论】:
-
想改写吗?这有点令人困惑......
-
也不清楚为什么
myclass->my_function()首先变成run(),但echo '..'在第二部分。 -
我在评论中解释了“运行”。它包括文件中的内容。我真正需要做的是:
echo preg_replace($pattern, $replacement, $string);其中 pattern 找到 my_function 行,replacement 是包含的内容(通过执行 my_include( 'param1','param2')。 -
我觉得你需要
eval()php.net/manual/en/function.eval.php -
我发了一个新帖子,这次简单多了:stackoverflow.com/questions/4724564/…
标签: php html parsing preg-replace