【发布时间】:2014-02-15 23:24:02
【问题描述】:
我已经为此工作了一整天,但找不到可以像我有字符串一样替换 php 中字符串中的子字符串的解决方案
'<div>
<h2>this is <span>String</span> found in h2 tag</h2>
<p>Hello World</p>
<h2>this is <span>String</span> found in h2 tag</h2>
<p>Hello Universe</p>
<h2>this is <span>String</span> found in h2 tag</h2>
</div>'
我想获取 h2 中的每个字符串,然后执行一些 htmlentity 替换,例如
$str = 'this is <span>String</span> found in h2 tag';
$sanitized = htmlspecialchars($str,ENT_QUOTES);
然后输出完整的字符串但被替换。
如何实现?
<div>
<h2>this is <span>String</span> found in h2 tag</h2>
<p><b>Hello</b> World</p>
<h3>this is <span>String</span> found in h2 tag</h3>
<p><b>hello</b> Universe</p>
<h2>this is <span>String</span> found in h2 tag</h2>
</div>
【问题讨论】:
-
看看这是否有帮助us2.php.net/book.dom
-
不能有一些简单的解决方案来获取子字符串并执行一些计算然后替换吗?这似乎我将不得不返工很多。事实是,我现在已经变得愚蠢了,因为我已经为此投入了一整天的时间
-
虽然使用 substr 看起来更简单,但 dom 绝对是正确的解决方案。将失去的一天视为一种学习经历。
标签: php regex string dom substring