Moon
Moon

Reputation: 22565

How to remove custom tag in PHP?

I am trying to remove following tag from string.

[caption id="attachment_9" align="alignleft" width="137" caption="test"][/caption]

How do I remove it using php's preg_replace?

I tried several regular expression, but failed all.

Upvotes: 0

Views: 1972

Answers (2)

Amber
Amber

Reputation: 526573

$output_string = preg_replace('#\[caption[^\]]*\](.*?)\[/caption\]#m', "$1", $input_string)

or if you also want to remove anything between the opening and closing tag, just change "$1" to "".

Upvotes: 4

mikegreenberg
mikegreenberg

Reputation: 1421

Here you are:

Tested here: http://www.pagecolumn.com/tool/pregtest.htm

<?php 
$ptn = "/\[caption.+caption\]/";
$str = "Otherstuff[caption id=\"attachment_9\" align=\"alignleft\" width=\"137\" caption=\"test\"][/caption]Something else";
$rpltxt = "@";
echo preg_replace($ptn, $rpltxt, $str);
?>

Upvotes: 0

Related Questions