UPDATE: If you are using PHP 5.4, and get an error installing the pear bbcode module, try installing “bbcode-1.0.3b1” rather than “bbcode”. See this thread for more information:
If you have a site that allows users to create their own content, you’re going to need bbcodes(aka “Bulletin Board Codes”) to make your and their life easier.
bbcodes, or html shortcuts, are the tags people use in their content, instead of standard HTML tags, that are used to markup some text, such as bolding a paragraph, creating a link, etc. Everybody’s seen them, [u] for underlining, [b] for bold, etc.
I created a rudimentary bbcode system for my projects, but it didn’t handle nested bbcodes very well, and due to my lack of regular expression expertise, the function was quite a hog on processing large amounts of content.
So I decided to give up and try the PECL extension “bbcode” a try. I couldn’t find a lot of examples online about this extension, so I thought I’d put a few notes together here.
First, I use CentOS for my production environments, and I try to keep things tidy using RPM’s, so these instructions will be for that distribution, and the yum installer.
If you have a new enough version of PHP installed on your system, you may already have the PEAR/PECL libraries installed.
First, on CentOS, Fedora, and other RedHat type of distributions, see if the PEAR libraries are already installed.
yum list | grep ^php-pear.*installed
If PEAR is installed, you should see a list of installed packages. If not, you can install the required library with the command.
yum install php-pear
Next, you probably need the php-devel package to compile and install PECL extensions, so check to see if it’s installed
yum list | grep ^php-devel.*installed
And install it with this command if it isn’t already.
yum install php-devel
At this point, you’re ready to get and install any PECL extension, including the “bbcode” one we’re going to use.
Just for reference, this is the site on PECL for the bbcode extension:
To install the PECL bbcode extension, execute this command
pecl install bbcode
The process should download, compile, and install the bbcode extension into PHP. If you get an error about “PHPize missing”, it means that the php-devel package isn’t installed (see above), or something else is wrong with your environment.
After that point, you’re ready to use PECL’s bbcode extension in your own code.
Basically, in a nutshell, you create an array of bbcodes that you want to use for your content, and then pass your content through the bbcode parser to do its magic.
This page contains the documentation on using the bbcode library, but it may be easier to just provide an example.
First, create an array of the bbcodes you want to use, you can also create a callback method for a bbcode to do some extra processing that might be needed.
I’ve used this array of bbcodes in the past, it creates bbcodes for [u], [i], [b], [url], [img], [code], and "; echo bbcode_parse($parser,$mystr);
If everything worked, you should see your bbcode parsed, and displayed the way you want it to.
The PECL bbcode extension will also close any wandering open tags for you, although I’ve seen some peculiarities with that. Cleaning and sanitizing your content, and instituting a good bbcode system will help keep your content clean and safe.
You may also be interested in these PHP functions….