Preg_replace 条规则可更有效地定位

Preg_replace rules to target more eficiently

我有一些在 post 中动态生成的简码。

[简码 1][简码 2][简码 3] ... [简码 77]

我正在使用 WP 过滤功能对 content_save_pre 到 运行 一个 preg_match 内容并从中删除所有数字简码。

所以:

[简码 1] 变为 [简码]

...

[简码 77] 变为 [简码]

这是我现在用的

add_filter( 'content_save_pre' , 'preg_replace' , 10, 1);
funtion preg_replace ($content){
 $content = preg_replace('/shortcode .]/', 'shortcode]', $content);
 return $content;
}

我的问题是,在某些情况下,两位数的短代码不会更改。这样接缝就可以工作(有 2 个点),但肯定有更好的方法来定位数字,以便所有数字都被删除,不管家里有没有。

$content = preg_replace('/shortcode ..]/', 'shortcode]', $content);

使用这个:

$content = preg_replace('/shortcode \d+\]/', 'shortcode]', $content);