使用正则表达式删除字符串,只留下带有 img 标签的字符串 - PHP

Remove string with regexp leaving only ones with img tag inside - PHP

我有一个字符串,其中包含以前由 WP 格式化的内容,它包含几种类型的 [caption] 标签。示例:

[caption (attributes here)]some text[/caption]

[caption (attributes here)]©name and surname[/caption]

[caption id="attachment_49532" align="aligncenter" width="530"][/caption]

我只需要删除包含 © 后跟人名的那些。

我试过了:

preg_replace("/\[caption(.*?)\]©(.*?)\[\/caption\]/","",$string);

但只有当我只有一个 [caption] 标签作为输入字符串时它才能完成工作

$string = "[caption (parameters here)]©name and surname[/caption]";

而不是里面有更多元素的字符串。

我做错了什么?

启用 DOTALL 修饰符,这样它会在您的正则表达式中创建点以匹配换行符。

preg_replace('/\[caption\b.*?\]©.*?\[\/caption\]/su',"",$string);