Pages

Tuesday, January 10, 2012

Convert BBCode to HTML in PHP

function bb2html($message)
        {                               
            $preg = array(
                '/(?<!\\\\)\[COLOR(?::\w+)?=(.*?)\](.*?)\[\/COLOR(?::\w+)?\]/si'   => "<span style=\"color:\\1\">\\2</span>",
                '/(?<!\\\\)\[SIZE(?::\w+)?=(.*?)\](.*?)\[\/SIZE(?::\w+)?\]/si'     => "<span style=\"font-size:\\1\">\\2</span>",
                '/(?<!\\\\)\[FONT(?::\w+)?=(.*?)\](.*?)\[\/FONT(?::\w+)?\]/si'     => "<span style=\"font-family:\\1\">\\2</span>",
                '/(?<!\\\\)\[ALIGN(?::\w+)?=(.*?)\](.*?)\[\/ALIGN(?::\w+)?\]/si'   => "<div style=\"text-align:\\1\">\\2</div>",
                '/(?<!\\\\)\[B(?::\w+)?\](.*?)\[\/B(?::\w+)?\]/si'                 => "<span style=\"font-weight:bold\">\\1</span>",
                '/(?<!\\\\)\[I(?::\w+)?\](.*?)\[\/I(?::\w+)?\]/si'                 => "<span style=\"font-style:italic\">\\1</span>",
                '/(?<!\\\\)\[U(?::\w+)?\](.*?)\[\/U(?::\w+)?\]/si'                 => "<span style=\"text-decoration:underline\">\\1</span>",
                '/(?<!\\\\)\[CENTER(?::\w+)?\](.*?)\[\/CENTER(?::\w+)?\]/si'       => "<div style=\"text-align:center\">\\1</div>",
                               
                // [email]
                '/(?<!\\\\)\[EMAIL(?::\w+)?\](.*?)\[\/EMAIL(?::\w+)?\]/si'         => "<a href=\"mailto:\\1\" class=\"bb-email\">\\1</a>",
                '/(?<!\\\\)\[EMAIL(?::\w+)?=(.*?)\](.*?)\[\/EMAIL(?::\w+)?\]/si'   => "<a href=\"mailto:\\1\" class=\"bb-email\">\\2</a>",
                // [url]
                '/(?<!\\\\)\[URL(?::\w+)?\]www\.(.*?)\[\/URL(?::\w+)?\]/si'        => "<a href=\"http://www.\\1\" target=\"_blank\" class=\"bb-url\">\\1</a>",
                '/(?<!\\\\)\[URL(?::\w+)?\](.*?)\[\/URL(?::\w+)?\]/si'             => "<a href=\"\\1\" target=\"_blank\" class=\"bb-url\">\\1</a>",
                '/(?<!\\\\)\[URL(?::\w+)?=(.*?)?\](.*?)\[\/URL(?::\w+)?\]/si'      => "<a href=\"\\1\" target=\"_blank\" class=\"bb-url\">\\2</a>",
                // [img]
                '/(?<!\\\\)\[IMG(?::\w+)?\](.*?)\[\/IMG(?::\w+)?\]/si'             => "<img src=\"\\1\" alt=\"\\1\" class=\"bb-image\" />",
                '/(?<!\\\\)\[IMG(?::\w+)?=(.*?)x(.*?)\](.*?)\[\/IMG(?::\w+)?\]/si' => "<img width=\"\\1\" height=\"\\2\" src=\"\\3\" alt=\"\\3\" class=\"bb-image\" />",
                                       
                // [list]
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[\*(?::\w+)?\](.*?)(?=(?:\s*<br\s*\/?>\s*)?\[\*|(?:\s*<br\s*\/?>\s*)?\[\/?LIST)/si' => "\n<li class=\"bb-listitem\">\\1</li>",
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[\/LIST(:(?!u|o)\w+)?\](?:<br\s*\/?>)?/si'    => "\n</ul>",
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[\/LIST:u(:\w+)?\](?:<br\s*\/?>)?/si'         => "\n</ul>",
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[\/LIST:o(:\w+)?\](?:<br\s*\/?>)?/si'         => "\n</ol>",
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[LIST(:(?!u|o)\w+)?\]\s*(?:<br\s*\/?>)?/si'   => "\n<ul class=\"bb-list-unordered\">",
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[LIST:u(:\w+)?\]\s*(?:<br\s*\/?>)?/si'        => "\n<ul class=\"bb-list-unordered\">",
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[LIST:o(:\w+)?\]\s*(?:<br\s*\/?>)?/si'        => "\n<ol class=\"bb-list-ordered\">",
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[LIST(?::o)?(:\w+)?=1\]\s*(?:<br\s*\/?>)?/si' => "\n<ol class=\"bb-list-ordered,bb-list-ordered-d\">",
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[LIST(?::o)?(:\w+)?=i\]\s*(?:<br\s*\/?>)?/s'  => "\n<ol class=\"bb-list-ordered,bb-list-ordered-lr\">",
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[LIST(?::o)?(:\w+)?=I\]\s*(?:<br\s*\/?>)?/s'  => "\n<ol class=\"bb-list-ordered,bb-list-ordered-ur\">",
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[LIST(?::o)?(:\w+)?=a\]\s*(?:<br\s*\/?>)?/s'  => "\n<ol class=\"bb-list-ordered,bb-list-ordered-la\">",
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[LIST(?::o)?(:\w+)?=A\]\s*(?:<br\s*\/?>)?/s'  => "\n<ol class=\"bb-list-ordered,bb-list-ordered-ua\">",
                                       
                //line breaks
                '/\n/'                                                               => "<br>",
                // escaped tags like \[b], \[color], \[url], ...
                '/\\\\(\[\/?\w+(?::\w+)*\])/'                                      => "\\1"
                               
            );
                               
            $message = preg_replace(array_keys($preg), array_values($preg), $message);
            return $message;
        }


// Call Function bb2html and echo

$htmltext = bb2html($message);
echo $htmltext;

No comments:

Post a Comment