Location: PHPKode > scripts > Light BBCode Parser > light-bbcode-parser/lightbbcodeparser.class.php
<?php
/**
* Class for parsing BBCode
*
* This class can be use for parsing common BBCode tags.
*
* @license	GNU General Public License 
* @author	Nikola Posa, www.nikolaposa.in.rs
*/
class LightBBCodeParser {
	
	//array of bbcode patterns 
	protected $patterns = array
	(
		'/\[b\](.+)\[\/b\]/Uis', 
		'/\[i\](.+)\[\/i\]/Uis',
		'/\[u\](.+)\[\/u\]/Uis',
		'/\[s\](.+)\[\/s\]/Uis',
		'/\[url=(.+)\](.+)\[\/url\]/Ui',
		'/\[img\](.+)\[\/img\]/Ui',	
		'/\[code\](.+)\[\/code\]/Uis',
		'/\[color=(\#[0-9a-f]{6}|[a-z]+)\](.+)\[\/color\]/Ui', 
		'/\[color=(\#[0-9a-f]{6}|[a-z]+)\](.+)\[\/color\]/Uis'
	);
	
	//array of HTML tags that correspond to bbcode patterns
	protected $replacements = array
	(
		'<b>\1</b>', 
		'<i>\1</i>',
		'<u>\1</u>',
		'<s>\1</s>',
		'<a href = "\1" target = "_blank">\2</a>',
		'<img src = "\1" alt = "Image" />',
		'<pre>\1</pre>',
		'<span style = "color: \1;">\2</span>',
		'<div style = "color: \1;">\2</div>'
	);
	
	/**
	* This function converts bbcode to (x)HTML tags.
	*
	* @param string Text that will be parsed.
	* @return string
	*/
	public function bbc2html($subject){
		$subject = nl2br($subject);
		
		$subject = preg_replace($this->patterns, $this->replacements, $subject);
			
		return $subject;
	}
}
?>
Return current item: Light BBCode Parser