开发者

How to remove custom tag in PHP?

开发者 https://www.devze.com 2023-01-06 01:32 出处:网络
I am trying to remove following tag from string. [caption id=\"attachment_9开发者_如何转开发\" align=\"alignleft\" width=\"137\" caption=\"test\"][/caption]

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.


$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 "".


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);
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消