Dyl*_*oss 4 php regex wordpress
我试图从我的Wordpress生成的数据库的文本字符串中删除一些HTML.
我要这个:
Marnie Stanton led us through the process first and then everyone went crazy.
[caption id="attachment_76" align="alignnone" width="191"] One of the work stations[/caption]
[caption id="attachment_78" align="alignnone" width="300"] The group is getting some great results[/caption]
[caption id="attachment_83" align="alignnone" width="224"] You can see the prints multiplying[/caption]
Run Code Online (Sandbox Code Playgroud)
变成这个:
Marnie Stanton led us through the process first and then everyone went crazy.
Run Code Online (Sandbox Code Playgroud)
所以我想要的是从第一个[caption]到最后一个[/caption]被删除的所有内容.
我从这开始:
(\[caption\s+?[^]]+\])
Run Code Online (Sandbox Code Playgroud)
这只删除了第一个标签.
你可能想要使用这样的东西
$string = 'Marnie Stanton led us through the process first and then everyone went crazy.
[caption id="attachment_76" align="alignnone" width="191"] One of the work stations[/caption]
[caption id="attachment_78" align="alignnone" width="300"] The group is getting some great results[/caption]
I want to keep this !
[caption id="attachment_83" align="alignnone" width="224"] You can see the prints multiplying[/caption]';
$new_string = preg_replace('#\s*\[caption[^]]*\].*?\[/caption\]\s*#is', '', $string);
echo $new_string;
Run Code Online (Sandbox Code Playgroud)
输出:
Marnie Stanton首先引导我们完成整个过程然后每个人都疯了.我想保留这个!
说明:
is:i表示匹配不区分大小写,s表示将新行与点匹配.\s* :匹配空格0次或更多次\[caption : 比赛 [caption[^]]*:匹配除]0或更多次以外的任何内容\] : 比赛 ].*?\[/caption\]:匹配任何东西,直到[/caption]找到(并匹配[/caption])\s* :匹配空格0次或更多次