匹配preg_replace中的所有<strong>标签

时间:2019-02-08 15:13:09

标签: php regex wordpress preg-replace

我们是PHP中的正则表达式(preg_replace)的新手,让它完全按照我们想要的方式工作有点麻烦。

例如,我们有这样的HTML代码:

<h2><strong>Automatic Writing</strong> <strong>– A Conduit For An Entity From Another World?</strong></h2>

我们要删除H2内部的所有样式标签(甚至也要匹配H3 / H4 / H5标签)。

到目前为止,我们已经构建了以下代码(我们正在与Wordpress集成):

function removebolding($content)
        {
            $content =
                preg_replace('/(<h([1-6])[^>]*>)\s?<strong>(.*)?<\/strong>\s?(<\/h\2>)/', "$1$3$4", $content);
            return $content;
        }

        add_filter('the_content', 'removebolding');

这确实有效,但是,它只删除了第一个“ strong”标签-​​我们剩下:

<h2>Automatic Writing <strong>– A Conduit For An Entity From Another World?</strong></h2>

我们如何匹配/删除所有“强”标签?另外,也许我们可以简单地提取标题标签的内容,运行strip_tags函数,然后替换为输出?

任何帮助,建议和代码示例都应先感谢。

非常感谢。

1 个答案:

答案 0 :(得分:1)

您可以使用

preg_replace_callback('~<h([1-6])>.*?</h\1>~is', function($m) { 
    return preg_replace('~</?strong>~i', '', $m[0]); }
, $s)

输出:<h2>Automatic Writing – A Conduit For An Entity From Another World?</h2>

正则表达式的性能可以这样增强:

'~<h([1-6])>[^<]*(?:<(?!/h\1>[^<]*)*</h\1>~i'

请参见PHP demo

  • ~<h([1-6])>.*?</h\1>~s与任何h标记匹配,并且它们之间包含任何文本
  • preg_replace('~</?strong>~i', '', $m[0])仅在<strong>中的主正则表达式匹配值中删除所有</strong>$m[0]标记。
相关问题