Tutorio.com

Simple and Complex BBcode with PHP
This is a practical guide on creating simple and complex bbcode with php.

Part 1: Simple bb code

This techinque for simple bb code can be used to easily replace tags with no attributes like the bold and italic tags.

$string = "I am so [b]cool[/b] "
$bb-replace = array('[b]','[/b]','[i]',[/i]);
$bb-replacements = array ('','','','');
$string = str_replace($bb-replace,$bb-replacements,$string);

Two arrays are defined one for the bb code and another one for the html that will replace the bbcode and then the str_replace function is used to replace the html text for the bbcode in the string.

The same technique may also be used for things such as creating a swear filters, smilies and emoticons etc..

Part 2: Complex BB code

The previous technique for creating bb code works well for things like bold and italic tags, but its not good for creating more complex things like bb code for hyperlinks or image tags.

$string = 'This is [b]cool[/b] - [url=http://www.tutorio.com]Tutorio.com Tutorials[/url] 
$bb-replace = array ('/(\[[Bb]\])(.+)(\[\/[Bb]\])/','/(\[url=)(.+)(\])(.+)(\[\/url\])/');
$bb-replacements = array ('<b>\\2</b>','<a href="\\2">\\4</a>');
$string = preg_replace($bb-replace, $bb-replacements, $string);
print $string;

This is somewhat similar to the first example in that it still contains two arrays one containing the information for the bb code you need to replace and the other for the html text that will replace it. The difference is that this time instead of using the str_replace, this technique use preg_replace and regex. In this example the BBcode for bold text and the URL hyperlinks is replaced with the correct html tags.

Regex is a very powerful (and somewhat confusing) set of rules to create patterns for string manipulation, if you are into programming you are probably familiar with it already. The BBcode replacement pattern used for the url tags in this tutorial can be broken down into 5 parts, each part is enclosed within a round bracket. The preg function will find and replace either the exact text in each of these round brackets or match the text to the rules in these brackets.

(\[url=) This part matches '[url=' in the BBcode. the reason There is a backslash in front of the square brackets is that square brackets a special charecter in regex. Note that parts 3 and 5 work in the same kind of way.

(.+) This part would tell regex what characters to match, the dot matches almost every character and the + sign will means the number of characters has to be greater than zero, its used in parts 2 and 4. The reason it is used here is because we don't know the URL or the name of the page that the user is going to link to, .

<a href="\\2">\\4</a> Here parts 2 and 4 (url and title) are extracted and put inside an HTML hyperlink which replaces the BBcode.

BBcode is very popular in PHP scripts such as forums guestbooks and shoutboxes that wish to allow some user styling while limiting the potential for abuse that comes with giving users full access to HTML

Rating(187) +-
Tutorio.com. Privacy Policy, Contact