Welcome to WebmasterWorld Guest from 54.145.117.127

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

preg replace help needed

     
9:24 pm on Dec 2, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Apr 18, 2006
posts: 115
votes: 0


I'm trying to remove all bbcode tags in my forum post with the following:

$patterns[0] = "/\[.+\]/i";
$replacements[0] = "";
$postText = preg_replace($patterns, $replacements, $postText);

However when running into the following it will match the entire string below (and thereby remove it):


[tag1][tag2][/tag2][/tag1]

Instead it should have matched tag1 then tag2 then... and so on.

Any input on what I can do to accomplish this without trying to match each individual tag?

Thanks in advance.

7:44 am on Dec 3, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Apr 18, 2006
posts: 115
votes: 0


it seems i need to change the pattern from greedy to ungreedy by using the ? quantifier or /U.
8:25 am on Dec 3, 2008 (gmt 0)

Senior Member from MY 

WebmasterWorld Senior Member vincevincevince is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Apr 1, 2003
posts:4847
votes: 0


Yes, you're right - add ? after the +