<?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
    (
		'/\n/Ui',
		'/\[b\]/Ui',
		'/\[\/b\]/Ui',
		'/\[i\]/Ui',
		'/\[\/i\]/Ui',
		'/\[u\]/Ui',
		'/\[\/u\]/Ui',
		'/\[s\]/Ui',
		'/\[\/s\]/Ui',
		'/\[size=([0-9]+)\]/Ui',
		'/\[\/size\]/Ui',
		'/\[url=([^\]]+)\](.*?)\[\/url\]/Ui',
		'/\[url\](.*?)\[\/url\]/Ui',
		'/\[img\](.*?)\[\/img\]/Ui',
		'/\[color=([^\]]*?)\]([^\[]*?)\[\/color\]/Ui',
		'/\[code\](.*?)\[\/code\]/Ui',
		'/\[quote.*?\](.*?)\[\/quote\]/Ui'
    );

    //array of HTML tags that correspond to bbcode patterns
    protected $replacements = array
    (
		'<br />',
		'<span class="bbcodeB">',
		'</span>',
		'<span class="bbcodeI">',
		'</span>',
		'<span class="bbcodeU">',
		'</span>',
		'<span class="bbcodeS">',
		'</span>',
		'<span style="font-size: \1px;">',
		'</span>',
		'<a href="\1">\2</a>',
		'<a href="\1">\1</a>',
		'<img src="\1" />',
		'<span style="color: \1">\2</span>',
		'<span class="bbcodeCode">\1</span>',
		'<span class="bbcodeQuote">\1</span>'
    );

    /**
    * This function converts bbcode to (x)HTML tags.
    *
    * @param string Text that will be parsed.
    * @return string
    */
    public function bbc2html($subject){
        $subject = preg_replace($this->patterns, $this->replacements, $subject);

        return $subject;
    }
}
?> 