替换php中字符串中标记之间的子串

时间:2014-02-15 23:24:02

标签: php regex string dom substring

我一直在研究这一天,但找不到一个解决方案,我可以在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 &lt;span&gt;String&lt;/span&gt; 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 &lt;span&gt;String&lt;/span&gt; found in h2 tag</h2>
</div>

1 个答案:

答案 0 :(得分:3)

您可以使用preg_replace_callback()。匹配<h2>标记的正则表达式为:

/<h2>(.+?)<\/h2>/  

如果您想匹配所有<hx>代码,请改为使用以下代码:

/<h([1-6])(.*?)<\/h\1>/ 

在回调函数中,您可以更改匹配的字符串。例如:

$html = <<< EOH

<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>

EOH;

$html = preg_replace_callback("/<h2>(.+?)<\/h2>/", function($matches) {
    /* Convert content of <h2> tags to HTML entities. */
    $altered =  htmlspecialchars($matches[1], ENT_QUOTES);

    /* Put the converted content back inside <h2> tag and return it. */
    return str_replace($matches[1], $altered, $matches[0]);
}, $html);

$html = preg_replace_callback("/<p>(.+?)<\/p>/", function($matches) {
    /* Make match bold. */
    $altered = "<b>" . $matches[1] . "</b>";

    /* Put the converted content back inside <p> tag and return it. */
    return str_replace($matches[1], $altered, $matches[0]);
}, $html);

print $html;

上述脚本的输出是:

<div>
   <h2>this is &lt;span&gt;String&lt;/span&gt; found in h2 tag</h2>
   <p><b>Hello World</b></p>
   <h2>this is &lt;span&gt;String&lt;/span&gt; found in h2 tag</h2>
   <p><b>Hello Universe</b></p>
   <h2>this is &lt;span&gt;String&lt;/span&gt; found in h2 tag</h2>
</div>