简体   繁体   English

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

[英]Matching all <strong> tags in preg_replace

We are new to regex (preg_replace) in PHP and are having a little trouble getting it to do exactly what we want.我们是 PHP 中正则表达式 (preg_replace) 的新手,并且在让它完全按照我们想要的方式执行时遇到了一些麻烦。

We have, for example, HTML code like this:例如,我们有这样的 HTML 代码:

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

We want to remove all styling tags inside the H2 (and even match H3/H4/H5 tags too).我们想删除 H2 中的所有样式标签(甚至匹配 H3/H4/H5 标签)。

We have constructed the following code so far (we are integrating with Wordpress):到目前为止,我们已经构建了以下代码(我们正在与 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');

This does work, however, it only removes the first 'strong' tag - we are left with:这确实有效,但是,它只会删除第一个“强”标签——我们只剩下:

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

How can we match/remove all 'strong' tags?我们如何匹配/删除所有“强”标签? Also, perhaps we could simply extract the contents of the heading tags, run a strip_tags function and then replace with the output?另外,也许我们可以简单地提取标题标签的内容,运行一个 strip_tags 函数,然后用输出替换?

Any help, suggestions, and code samples are gratefully appreciated in advance.提前感谢任何帮助、建议和代码示例。

Many thanks.非常感谢。

You may use您可以使用

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

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

The regex performance may be enhanced like this:可以像这样增强正则表达式的性能:

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

See the PHP demo .请参阅PHP 演示

  • ~<h([1-6])>.*?</h\\1>~s matches any h tags, with any text in between them ~<h([1-6])>.*?</h\\1>~s匹配任何h标签,它们之间有任何文本
  • preg_replace('~</?strong>~i', '', $m[0]) removes all <strong> and </strong> tags only in the main regex match value, in $m[0] . preg_replace('~</?strong>~i', '', $m[0])仅在$m[0]中的主要正则表达式匹配值中删除所有<strong></strong>标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM