View Full Version : Stop [code] tag from parsing other BBCode

Cloud Ghost
09-11-2011, 02:56 AM
Hello, I recently built a comment system and I added BBCode to it today but I ran into a problem. I want to have a [code] tag so I used preg_replace() with something similar to this:

$message = preg_replace("/(\[b\])(.+)(\[\/b\])/s", "<b>$2</b>", $message);

The problem is, I also have smilies and I use str_replace() for those so the smilies (and other BBCode) will get parsed inside the [code] tag which I do not want. I don't want the user to be able to disable BBCode or smilies so is there a way for the [code] tag to be processed differently than the other tags and stop smilies and other BBCode from being parsed?

09-11-2011, 09:54 PM
You may need to separate chunks of the user's text into different strings depending on what BBCode is used where, and run the strings through the appropriate preg_replace().

09-11-2011, 11:31 PM
So, what I think you want is a preg_replace etc. that doesn't work for information encased inside [code] tags?

You'll probably need a recursive solution to this. You'll need to use strpos() to find an occurence of [ code] (based on an offset passed into the function), then preg_replace up to the code tag using substr(), then find [ /code] using an offset based on the strpos() of it's respective [ code], and run the function again, passing the new offset as the [ /code].

Let me know how you get on, I'm here to help. Recursion isn't easy to get, and if you haven't encountered it before I'll be more than happy to help futher.