You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用preg_match_all匹配含特殊字符的精确内容并替换为带类的span

Fixing the Exact Match & Replacement for Your WordPress Content Cleaner

Let's break down what's going wrong with your current function and fix it properly:

Key Issues in Your Current Code

  • Your regex matches all <strong> tags instead of only those containing exactly * * *
  • The * character is a special regex metacharacter—you need to escape it to match the literal asterisks
  • Your function stops after replacing the first match, leaving any additional instances untouched
  • You're using str_replace in a loop which is inefficient and error-prone for regex-based replacements

Updated Function with Correct Regex & Logic

Here's the optimized version that addresses all these problems:

function content_format_stars($str = null) {
    if (empty($str)) {
        return $str;
    }

    // Target the exact escaped HTML pattern: <strong>* * *</strong> (as HTML entities)
    // \s* allows for optional whitespace around the asterisks (adjust if you need strict matching)
    $pattern = '#&lt;strong[^&gt;]*&gt;\s*\*\s*\*\s*\*\s*&lt;/strong&gt;#';
    $replacement = '&lt;span class="crb-content-divider"&gt;* * *&lt;/span&gt;';

    // Replace ALL matching instances in one go with preg_replace
    return preg_replace($pattern, $replacement, $str);
}

What Changed & Why

  1. Precise Regex Pattern:

    • \* escapes the asterisk to match the literal character instead of using it as a regex wildcard
    • \s* accounts for any optional whitespace (spaces, tabs) around the asterisks—remove this if you need exact * * * with single spaces only
    • &lt;strong[^&gt;]*&gt; matches any <strong> tag (including those with attributes like class or id) without breaking the match
  2. Efficient Replacement:

    • Using preg_replace instead of preg_match_all + loop lets you replace all matching instances in one line, no need to manually iterate through matches
    • We avoid early return statements that stopped processing after the first match
  3. Robust Edge Handling:

    • The initial empty($str) check is simplified for readability
    • The function returns the original string if no matches are found (preg_replace does this automatically)

Example Usage

If your input content looks like this:

&lt;p&gt;Some regular text&lt;/p&gt;
&lt;strong&gt;* * *&lt;/strong&gt;
&lt;p&gt;More text&lt;/p&gt;
&lt;strong class="some-class"&gt;* * *&lt;/strong&gt;
&lt;strong&gt;This is a normal strong tag&lt;/strong&gt;

The output will be:

&lt;p&gt;Some regular text&lt;/p&gt;
&lt;span class="crb-content-divider"&gt;* * *&lt;/span&gt;
&lt;p&gt;More text&lt;/p&gt;
&lt;span class="crb-content-divider"&gt;* * *&lt;/span&gt;
&lt;strong&gt;This is a normal strong tag&lt;/strong&gt;

Strict Matching (Optional)

If you need to match only <strong> tags with exactly * * * (no extra whitespace, no attributes), use this pattern instead:

$pattern = '#&lt;strong&gt;\* \* \*&lt;/strong&gt;#';

内容的提问来源于stack exchange,提问作者Kyle Knight

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:52:09