可能的重复:
如何用PHP解析和处理HTML?
$content = "
<p>This is the first paragraph</p>
<p>This is the second paragraph</p>
<p>This is the third paragraph</p>";
给定上面的 html 内容字符串,我需要在第 N 段标记之后插入。
如何解析内容并插入给定的文本字符串,在第 2 段后说“hello world”?
你可以使用PHPexplode and implode功能。这是一个概念:
$content = "
<p>This is the first paragraph</p>
<p>This is the second paragraph</p>
<p>This is the third paragraph</p>";
$content_table = explode("<p>", $content);
这将创建$content_table
具有值:
Array ( [0] => [1] => This is the first paragraph
[2] => This is the second paragraph
[3] => This is the third paragraph
)
现在您可以使用以下命令更改任何您想要的内容$content_table[2]
对于第 2 段。例如,您可以执行以下操作:
$content_table[2] .= "hello world!";
完成后,只需再次将表内爆为字符串即可:
$content = implode($content_table, "<p>");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)