Using the PECL bbcode extension

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:

http://stackoverflow.com/questions/10696415/pecl-bbcode-package-fails-to-build

 

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:
http://pecl.php.net/package/bbcode

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.

http://www.php.net/manual/en/book.bbcode.php

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….

htmlspecialchars()
urlencode()
nl2br()
highlight-string()

9 Replies to “Using the PECL bbcode extension”

  1. It makes it a little easier by not having to worry that html tags are closed, and you can generate your own xhtml valid markup the way you want if you’re into that sort of thing.

  2. When you allow html tags, you have to make sure they get closed, or screw up other content on your site, then you also have to make sure there isn’t anything malicous in the tags. With bbcodes, you can strip all the html from the content, and parse the bbcode into legitimate html

  3. Never heard of PECL BBCodes, but I have heard of BBCodes and they’re REALLY useful on forums when you want to make your posts prettier. 😀

  4. I normally don’t stuff around with BBcode and all of that, i have never needed to use it on my MyBB powered forum before but i might start using it, this might come in handy.
    Thanks man!

  5. Thanks for this nice tutorial! Would you happen to know if there’s a way to trim() all BBCode content with a single handler?

  6. I was suggested PECL bbcodes but still have no clue if i installed it right or how exactly to use the code or reverse it to echo in textarea. Not many tutorials online

Leave a Reply