Php preg_match_all,查找包含字符串等特殊字符的精确匹配的所有实例,并添加类

Php preg_match_all,查找包含字符串等特殊字符的精确匹配的所有实例,并添加类,php,preg-match-all,Php,Preg Match All,我正在尝试创建一个函数,用于清理找到精确匹配的某些资源中心和中心实例,将其替换为一个范围,并向其中添加一个类。我有一个基本函数,但我似乎无法找出正确的语法,因为匹配项包含***字符 e、 g.*** 我已经尽可能的详细,我真的希望这一切都是有意义的,如果不是,请不要只是记下问题,请解释为什么我可以改进问题,帮助别人 在发表这篇评论之前,我已经查看了所有的堆栈溢出问题,因为我知道他们的问题很多都是基于preg_match_all,但没有一个真正适合我的情况。我正在努力找出当内容具有特殊字符时使用的

我正在尝试创建一个函数,用于清理找到精确匹配的某些资源中心和中心实例,将其替换为一个范围,并向其中添加一个类。我有一个基本函数,但我似乎无法找出正确的语法,因为匹配项包含***字符

e、 g.
***

我已经尽可能的详细,我真的希望这一切都是有意义的,如果不是,请不要只是记下问题,请解释为什么我可以改进问题,帮助别人

在发表这篇评论之前,我已经查看了所有的堆栈溢出问题,因为我知道他们的问题很多都是基于preg_match_all,但没有一个真正适合我的情况。我正在努力找出当内容具有特殊字符时使用的模式,我认为这是第一个问题,第二个问题是我下面的函数可能不是最好的方法,但作为一个业余爱好者,这是有意义的

请参阅下面的函数,这是我通过查看堆栈溢出和论坛上的其他答案所尝试的

function content_format_stars($str = null) {
      if( !empty($str) ) {

        $pattern = "#<strong[^>]*>(.*?)</strong>#";

        $matches = array();

        if(preg_match_all($pattern, $str, $matches, PREG_SET_ORDER) !== false){

          $key = '* * *';

          if($matches) {

            foreach($matches as $match) {

              if ($match[1] == $key) {

                $br = '<span class="crb-content-divider">* * *</span>';

                $new_str = str_replace($match[0], $br, $str);

                return $new_str;

              }

            }

          } else {
            return $str;
          }

        } else {
          return $str;
        }
      }
    }
function content\u format\u stars($str=null){
如果(!空($str)){
$pattern=“#]*>(.*)#”;
$matches=array();
if(preg\u match\u all($pattern,$str,$matches,preg\u SET\u ORDER)!==false){
$key='***';
如果($匹配){
foreach($matches作为$match进行匹配){
如果($match[1]=$key){
$br='***';
$new_str=str_replace($match[0],$br,$str);
返回$new_str;
}
}
}否则{
返回$str;
}
}否则{
返回$str;
}
}
}
预期的结果应该是,它会查看内容,并找到与此字符串正好匹配的任何字符串
***
,并将其替换为
***
。使用的字符串是数据库中的原始内容,这正是wordpress php模板中的_content()函数的输出

更新:

下面是$str输入的一个示例:

<p>The structure of the work will be familiar to those obliged to read books produced by columnists and broadcast-media figures: a series of mostly disconnected essays and vignettes repackaged as a monograph, lightly stitched up with newly written connective material and punctuated every fourth page or so by something the author doesn’t realize is hilariously stupid or obviously wrong. As with chainsaw sculpture, the process leaves its mark on the product: columns and essays are arranged in a particular order and then written through (sometimes by the author, often by a junior editor) two or three times until the recycled material smells fresh enough to put a cover and title on. Wright has spent decades writing about the politics and <strong>personalities</strong> of Texas, and this book is a kind of greatest-hits album underneath a thin wash of the now-familiar indignant moral hysteria induced in the NPR crowd by the Age of Trump.</p>

<p><strong>* * *</strong></p>
这部作品的结构对于那些不得不阅读专栏作家和广播媒体人物出版的书籍的人来说是熟悉的:一系列基本上不连贯的文章和小插曲被重新包装成专著,用新写的连接材料轻轻地缝合起来,每四页左右用作者没有意识到的东西加上标点,这是可笑的愚蠢或明显错误的。与链锯雕塑一样,这一过程在产品上留下了印记:专栏和文章按特定顺序排列,然后(有时由作者,通常由初级编辑)写两三遍,直到回收材料闻起来足够新鲜,可以贴上封面和标题。赖特花了几十年的时间来写得克萨斯州的政治和人物,这本书是一本最畅销的专辑,在特朗普时代在NPR人群中引发了一种现在很常见的愤怒的道德歇斯底里

***


以上只是一个片段,内容要长得多,在某些情况下,它们是
***
的多个实例。因此,当前函数将返回
的任何实例,根据我有限的基本理解,我构建了它,如上面所示

正如您所看到的那样,
$key='***'
,如果它是
$key='***',这不是已经在寻找带有空格的内容了吗?是的,但这实际上不起作用,它把所有的内容都弄乱了,我对这一点很陌生,我甚至不记得我的代码在达到这一点之前是什么样子:(这可能是6个多小时的测试和stackoverflow注释的结果。您能给出一个输入文本的示例以及您想要输出的内容吗?这有助于测试代码。@NigelRen,更新代码以显示示例。非常感谢各位的回复!