Reputation: 31
Need help with a regex. I have a big string of HTML and need to remove tags like this
[caption id="attachment_24612" align="alignnone" width="900"]
Each tag starts the same [caption id=
, and ends the same ]
... but what's in the middle will be unique each time. The regex code should look something like this, but I don't know how to pattern match the middle.
$sourceHTML = preg_replace(array("[caption id=", ""),"", $sourceHTML);
I just want to remove the entire placeholder tag.
Upvotes: 0
Views: 137
Reputation: 835
To delete those caption tags, you can use this regex:
\[caption[^]]+]
If your tags are on single line and you want to remove the blank line that the previous regex can leave, consider using this one:
\[caption[^]]+]\R?
The \R?
part will match possible new line characters right after the tag.
Example
$sourceHTML = preg_replace("/\[caption[^]]+]\R?/", "", $sourceHTML);
Upvotes: 2
Reputation: 1146
Use this instead:
$sourceHTML = preg_replace("/\[caption id=(.*?)\]/", "", $sourceHTML);
Upvotes: 0