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); Code (markup): However this also removes nested tags. Eg: 'tag1 begin, tag2 begin, tag2 end, tag1 end' <-- this will get completely deleted. 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.
Quick and dirty. '#\[[^\]]+\]#' Code (markup): A little less leanient '#\[/?[a-z0-9_ ]+\]#i' Code (markup): Also a quick note, if there's no alphabet characters (a-z) in your pattern, the "i" flag is absolutely useless.