This project is read-only.

PHP BBCode Parser [Code Included]

Dec 28, 2011 at 4:43 PM

This is our PHP BBCode parser, which you are welcome to use.

To use it, simply call parsePost, and set $m to your message. Implemented in CalicoBB.

	public function parsePost($m){
		// parses the post with bbCode, smilies and also checks the badwords
		// Make sure we cannot ever post HTML
		$m = htmlspecialchars($m, ENT_QUOTES);

		// Youtube (to remove, comment out :))
		$yt_f = '/\[youtube\](.*?)\[\/youtube\]/is';
		$yt_r = "<iframe title=\"YouTube video player\" width=\"480\" height=\"390\" src=\"$1?rel=0\" frameborder=\"0\" allowfullscreen>Youtube video - [url=$1]Watch now[/url]</iframe>";
		$m = preg_replace($yt_f,$yt_r,$m);
		// URLS, images and colours
		$url_f = array('/\[url\](.*?)\[\/url\]/is','/\[url=(.*?)\](.*?)\[\/url\]/is','/\[color=(.*?)\](.*?)\[\/color\]/is','/\[img\](.*?)\[\/img\]/is'); 
		$url_r = array('<a href="$1" target="_blank" title="$1">$1</a>','<a href="$1" target="_blank" title="$1">$2</a>',
			'<span style="color: $1">$2</span>','<img src="$1" alt="User posted image" title="User posted image" />');
		// replace
		$m = preg_replace($url_f,$url_r,$m);
		// Standard bbCode
		$bb_f = array('[b]','[/b]','[u]','[/u]','[i]','[/i]','[ul]','[/ul]','[li]','[/li]');
		$bb_r = array('<strong>','</strong>','<u>','</u>','<em>','</em>','<ul>','</ul>','<li>','</li>');
		$m = str_ireplace($bb_f,$bb_r,$m);

		// For Security...
		$m = str_ireplace("%3C%73%63%72%69%70%74","&lt;script",$m);
		$m = str_ireplace("%64%6F%63%75%6D%65%6E%74%2E%63%6F%6F%6B%69%65","document&#46;cookie",$m);
		$m = preg_replace("#javascript\:#is","java script:",$m);
		$m = preg_replace("#vbscript\:#is","vb script:",$m);
		$m = str_ireplace("`","&#96;",$m);
		$m = preg_replace("#moz\-binding:#is","moz binding:",$m);
		$m = str_ireplace("<script","&lt;script",$m);
		$m = str_ireplace("&#8238;",'',$m);
		// now, we can send it back
		return $m;