简体   繁体   English

PHP-使用Regex模式删除两者之间的简码和内容

[英]PHP - Remove Shortcodes and Content in between with Regex Pattern

I have the following string: 我有以下字符串:

$text = My example text [shortcode_name]content of shortcode[/shortcode_name] is cool.

And the following pattern: 和以下模式:

$pattern= '/\[(\/?shortcode_name.*?(?=\]))\]/';

It gives me the following result: 它给我以下结果:

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.

Here you go: 干得好:

$pattern= '/\[(shortcode_name)\].*?\[\/\1\] ?/';

Explanation: 说明:

  • match [ 比赛 [
  • followed by "shortcode_name" (captured within (...) for reuse in a later step, see below) 后跟“ shortcode_name”(捕获在(...) ,以在以后的步骤中重复使用,请参见下文)
  • followed by ] 其次是 ]
  • followed by anything, non-greedily (important!) 其次是非贪婪的(重要!)
  • followed by [/ 其次是 [/
  • followed by what was matched in (...) earlier, in this example "shortcode_name" 然后是前面(...)匹配的内容,在此示例中为“ shortcode_name”
  • followed by ] 其次是 ]
  • followed by 1 or zero space 后跟1个或零个空格

This could maybe work as your regex: 这也许可以作为您的正则表达式工作:

/\[(\w+)\].*\[\/(\1)\]/

you find the first square bracket tag of any name, and grep the content in between, and then use lookback to find the closing square bracket. 您可以找到任何名称的第一个方括号标记,并在其之间使用grep内容,然后使用回溯来查找右方括号。 I only tested it with javascript, but it should work. 我只用javascript测试过,但应该可以。 Then you should use another regex to replace multiple whitespace for one whitespace :) 然后,您应该使用另一个正则表达式将一个空格替换为多个空格:)

You just accepted another answer while I was working on my solution, no problem :) but I still want to post my answer, as it can be more flexible if you have multiple shortcodes: 当我在研究解决方案时,您刚刚接受了另一个答案,没问题:)但我仍想发布我的答案,因为如果您有多个简码,它可以更加灵活:

<?php

$shortCodes = array("shortcode_name", "shortcode_name2", "shortcode_name3");

$text = <<< EOF
My example text [shortcode_name]content of shortcode[/shortcode_name] is cool.
another example text [shortcode_name2]content of shortcode[/shortcode_name2] is cool.
Yet another example  [shortcode_name3]content of shortcode[/shortcode_name3] is cool.
EOF;

foreach($shortCodes as $shortCode){
   $text = preg_replace("%\[$shortCode\].*?\[/$shortCode\]%i", '', $text);      
   $text = preg_replace('/\s{2,}/i', ' ', $text); //remove 2 or more spaces

}

echo $text;

OUTPUT: 输出:

My example text is cool.
another example text is cool.
Yet another example is cool.

Demo: http://ideone.com/VhL5vm 演示: http //ideone.com/VhL5vm

Try my version it will take care the newline as well. 试用我的版本,它也会注意换行符。

$text = "My example text [shortcode_name]content of shortcode[/shortcode_name] is cool.";
$text = "My example text [shortcode_name]content of 
shortcode[/shortcode_name] is cool.";

$pattern= '/\[(shortcode_name)\](.|\s)*?\[\/\1\]/'; 
echo preg_replace($pattern,'' ,$text); 

Output 输出量
My example text is cool. 我的示例文字很酷。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM