Luke Cassady-Dorion
Luke Cassady-Dorion

Reputation: 31

Remove specific square brace placeholder in text: "[caption ...]"

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

Answers (2)

Niitaku
Niitaku

Reputation: 835

To delete those caption tags, you can use this regex:

\[caption[^]]+]

See the demo

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.

See the demo

Example

$sourceHTML = preg_replace("/\[caption[^]]+]\R?/", "", $sourceHTML);

Upvotes: 2

Hossam
Hossam

Reputation: 1146

Use this instead:

$sourceHTML = preg_replace("/\[caption id=(.*?)\]/", "", $sourceHTML);

Upvotes: 0

Related Questions