UltraMega Blog

Creating a BBCode Parser

Have you ever wanted to implement BBCode, the special formatting codes used by forums, into your own PHP scripts? Well, it's actually pretty easy to accomplish using some simple regular expressions and the preg_replace PHP function. This mini-tutorial will show you how to create a function that you can use on any string to convert BBCode into its XHTML equivalent.

The advantage to using BBCode instead of allowing XHTML in user input is that it allows users to safely format their content without the risk of invalid code breaking the page formatting. It also tends to be easier to understand BBCode over XHTML due to its simplified syntax.