本文介绍了PHP - 使用正则表达式模式删除短代码和内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下字符串:
$text = My example text [shortcode_name]content of shortcode[/shortcode_name] is cool.
还有以下模式:
$pattern= '/\[(\/?shortcode_name.*?(?=\]))\]/';
它给了我以下结果:
preg_replace($pattern,'',$text);
My example text content of shortcode is cool.
这就像删除短代码的魅力.
我正在寻找的是删除短代码和其间的所有内容.
我要找的结果是:
This works like a charm to remove the shortcode.
What I'm looking for is to remove the shortcode and all content in between.
The result that I'm looking for is:
My example text is cool.
推荐答案
给你:
$pattern= '/\[(shortcode_name)\].*?\[\/\1\] ?/';
说明:
- 匹配 [
- 后跟shortcode_name"(在
(...)
中捕获,以便在后面的步骤中重用,见下文) - 其次是]
- 跟随任何东西,非贪婪(重要!)
- 其次是 [/
- 接着是前面
(...)
中匹配的内容,在这个例子中是shortcode_name" - 其次是]
- 后跟 1 个或零个空格
- match [
- followed by "shortcode_name" (captured within
(...)
for reuse in a later step, see below) - followed by ]
- followed by anything, non-greedily (important!)
- followed by [/
- followed by what was matched in
(...)
earlier, in this example "shortcode_name" - followed by ]
- followed by 1 or zero space
这篇关于PHP - 使用正则表达式模式删除短代码和内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!