Snippet: bbcode

April 6, 2009
Tags: snippets, php

User generated content has become the backbone to social networking and information sites, and has created bigger and bigger headaches for developers alike trying to accomadate such needs.

One rule that most developers go by is to use a white list of allowed tags rather than a black list of forbidden tags. The reason to go with a white list, and disable everything else, is that html is forever evolving, which means forever updating your black list of tags, instead of the occasianal addition to the white list.

Now, if your smart, you will just go grab an open source project like Wordpress or phpBB which will control and manage user content for you and leave you to do whatever else you need to do.

But, since you like to build projects from scratch(a.k.a enjoy pain), there are various ways to limit what users can display on your site. One way is through site defined html code, or bbcode.

Bulletin Board Code or BBCode is a lightweight markup language used to format posts in many message boards... BBCode was devised to provide a safer, easier and more limited way of allowing users to format their messages. Programmer convenience was certainly another factor, as BBCode is very simple to implement.

Below I have written a small, simple function that converts basic bbcode to html. Go ahead and test it out for yourself


Allowed Tags: (Click to add)
  • [b]Bold Text[/b]
  • [i]Italic Text[/i]
  • [u]Underlined Text[/u]
  • [s]Strike Through Text[/s]
  • [url=http://www.codenothing.com]Linked Text[/url]
  • [hex=FF0000]Hex Colored Text[/hex]
  • [color=red]Colored Text[/color]
  • [sup]Super-Script Text[/sup]
  • [sub]Sub-Script Text[/sub]
  • [mail=corey@codenothing.com]Mail-Linked Text[/mail]
  • [center]Centered Text[/center]
  • [right]Right-Aligned Text[/right]
  • [left]Left-Aligned Text[/left]
Input Text


Obviously there are far more tags to choose from out there, but I just wanted to provide the basics to get you started. No package this time, just the function below.

/**
 * bbcode - Converts bbcode to html
 * April 6, 2009
 * Corey Hart @ http://www.codenothing.com
 *
 * Safely creates html from user generated code using
 * predefined tags.
 */

function bbcode($body){
	global $database;
	// Convert newline characters to break lines
	$body = nl2br($body);

	// Search Strings
	$search = array(
		"(\[b\](.*?)\[\/b])is",
		"(\[i\](.*?)\[\/i\])is",
		"(\[u\](.*?)\[\/u\])is",
		"(\[s\](.*?)\[\/s\])is",
		"(\[url\=http\:\/\/([^]]+)\](.*?)\[\/url\])is",
		"(\[url\=([^]]+)\](.*?)\[\/url\])is",
		"(\[hex\=([a-zA-Z0-9]{6})\](.*?)\[\/hex\])is",
		"(\[color\=([a-z]{3,12})\](.*?)\[\/color\])is",
		"(\[sup\](.*?)\[\/sup\])is",
		"(\[sub\](.*?)\[\/sub\])is",
		"(\[mail\=(.*?)\](.*?)\[\/mail\])is",
		"(\[center\](.*?)\[\/center\])is",
		"(\[right\](.*?)\[\/right\])is",
		"(\[left\](.*?)\[\/left\])is",
	);

	// Replacement Strings
	$replace = array(
		"<b>$1</b>",
		"<i>$1</i>",
		"<u>$1</u>",
		"<s>$1</s>",
		"<a href='http://$1' target='_blank'>$2</a>",
		"<a href='http://$1' target='_blank'>$2</a>",
		"<span style='color:#$1'>$2</span>",
		"<span style='color:$1'>$2</span>",
		"<sup>$1</sup>",
		"<sub>$1</sub>",
		"<a href='mailto:$1'>$2</a>",
		"<center>$1</center>",
		"<div align='right'>$1</div>",
		"<div align='left'>$1</div>",
	);

	// Search through the body for the differen't BBcodes and replace them
	return preg_replace($search, $replace, $body);
}