1

I have a string that contains content previously formatted by WP and it contains few types of [caption] tags. Examples:

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

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

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

I need to remove only the ones that contain © followed by the name of the person.

I tried:

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

but it does the job only if as input string I have only one [caption] tag like

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

and not the string with more elements inside.

What am I doing wrong?

Community
  • 1
  • 1
somonek
  • 373
  • 1
  • 6
  • 13
  • http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – nha Jul 29 '15 at 16:23
  • There is regex for you https://www.regex101.com/r/oJ5nX6/1 – splash58 Jul 29 '15 at 16:26
  • 1
    Can you provide an example of `the string with more elements inside`? Do you mean `[caption (attributes here)][bold]test[/bold]©name and surname[/caption]`? – chris85 Jul 29 '15 at 16:30
  • What if `]©` is in one of the attribute values? –  Jul 29 '15 at 17:53

1 Answers1

0

Enable DOTALL modifier, so that it would make dot in your regex to match also the line breaks.

preg_replace('/\[caption\b.*?\]©.*?\[\/caption\]/su',"",$string);
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274