使用 PHP 和 preg_replace 拆分和替换内容而不是爆炸?
我有一个 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。结果需要再次解析。
简短示例 - 之后
再次解析整个内容,并用包含的内容替换 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 可以解决这个问题吗?如何?
I have a PHP + HTML code as a string containing functions. I want some function calls to be replaced with included content. Put together many files to one.
Short example - Before
<html>
<?php myclass->my_function('one', 'two', 'three'); ?>
<h1>Content in between</h1>
<?php myclass->my_function('1', '2'); ?>
</html>
Short example - Replacing content
<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>`
Short example - After
<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>
Notice that myclass is found in the included content. The result needs to be parsed again.
Short example - After that
The whole this is parsed again and it replaced the myclass with included content.
<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>
I tried to do this with explode but it went to complex. The two steps "Replacing content" and "After" might need to be done in one move.
Look at it as a string. Can preg_replace solve this? How?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
应该做你想做的事。
Should do what you want.