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-pearNext, 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-develAt 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 bbcodeThe 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 .
$bbcodes = array( 'b' => array( 'type' => BBCODE_TYPE_NOARG, 'open_tag' => '<b>', 'close_tag' => '</b>', ), 'u' => array( 'type' => BBCODE_TYPE_NOARG, 'open_tag' => '<u>', 'close_tag' => '</u>', ), 'i' => array( 'type' => BBCODE_TYPE_NOARG, 'open_tag' => '<i>', 'close_tag' => '</i>', ), 'img' => array( 'type' => BBCODE_TYPE_NOARG, 'open_tag' => '<img src="', 'close_tag' => '" />', ), 'url' => array( 'type' => BBCODE_TYPE_OPTARG, 'open_tag' => '<a href="{PARAM}">', 'close_tag' => '</a>', 'childs' => 'b,i,img', 'default_arg' => '{CONTENT}', ), 'youtube' => array( 'type' => BBCODE_TYPE_OPTARG, 'open_tag' => '<iframe width="425" height="349" src="http://youtube.com/embed/{PARAM}" frameborder="0" allowfulscreen>', 'close_tag' => '</iframe>', 'default_arg' => '{CONTENT}', ), 'code' => array( 'type' => BBCODE_TYPE_NOARG, 'content_handling' => 'code_highlight', 'childs' => '', ), );
You'll see the bbcode for [code] refer to a callback function ('content_handling') called code_highlight. I need that because I convert new lines to
html codes in my content, but I DON'T want to do that for anything within the [code] bbcode. Your results may vary on this practice, but this callback method works for me to get the [code] content the way I want it.
function code_highlight($content){ $content = trim(str_replace("<br />","",$content)); return highlight_string($content); }
You'll also notice the [code] bbcode doesn't list any "childs", this is because I don't want any bbcodes listed within the [code] bbcode to be parsed/converted to HTML entities. On the other hand thought, I DO allow [i], [u], and [img] bbcodes within the [url] bbcode.
Once that configuration of your bbcodes is set up, you just need to start using it with this example:
$parser = bbcode_create($bbcodes); $mystr = " Underline: [u]underlining[/u] Bold: [b]bolding[/b] Italics: [i]italics[/i] Link #1: [url]http://sneakydave.com[/url] Link #2: [url=http://sneakydave.com]my site[/url] Image: [img]http://www.google.com/logos/classicplus.png[/img] Code: [code]function() {some code here;}[/code] Youtube: [youtube]oHg5SJYRHA0[/youtube] "; 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....
Is that really true? I don’t get how it would make our life easier.
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.
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
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.
Wow! This trick is awesome! Thank you very much for this. It has helped me a lot.
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!
Thanks brother, these shortcuts are extremely useful.
Thanks for this nice tutorial! Would you happen to know if there’s a way to trim() all BBCode content with a single handler?