Preg_replace规则可以更有效地定位

时间:2015-01-22 11:34:22

标签: php regex preg-replace

我有一些在帖子中动态生成的短代码。

[短代码1] [短代码2] [短代码3] ... [短代码77]

我正在使用WP过滤器功能 content_save_pre 在内容上运行preg_match并从短代码中删除所有数字。

所以:

[短代码1]变为[短代码]

...

[shortcode 77]变为[shortcode]

这就是我现在使用的

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

我的问题是,在某些情况下,双位数字码不会改变。这种方式接缝工作(有两个点),但肯定有一种更好的方法来定位数字,以便所有这些都被删除,无论家里有什么。

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

1 个答案:

答案 0 :(得分:1)

使用此:

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