Parse BBCode code class BBCode.class.php:
<?phpclass BBCode {private static $simple _search = Array ('/\[b\] (. *?) \[\/b\]/is ', '/\[i\ ' (. *?) \[\/i\]/is ', '/\[u\ ' (. *?) \[\/u\]/is ', '/\[s\ ' (. *?) \[\/s\]/is ', '/\[size\= (. *?) \](.*?) \[\/size\]/is ', '/\[color\= (. *?) \](.*?) \[\/color\]/is ', '/\[center\ ' (. *?) \[\/center\]/is ', '/\[font\= (. *?) \](.*?) \[\/font\]/is ', '/\[align\= (left|center|right) \] (. *?) \[\/align\]/is ', '/\[url\ ' (. *?) \[\/url\]/is ', '/\[url\= (. *?) \](.*?) \[\/url\]/is ', '/\[mail\= (. *?) \](.*?) \[\/mail\]/is ', '/\[mail\ ' (. *?) \[\/mail\]/is ', '/\[img\ ' (. *?) \[\/img\]/is ', '/\[img\= (\d*?) X (\d*?) \](.*?) \[\/img\]/is ', '/\[img (. *?) \](.*?) \[\/img\]/ise ', '/\[quote\ ' (. *?) \[\/quote\]/is ', '/\[quote\= (. *?) \](.*?) \[\/quote\]/is ', '/\[code\ ' (. *?) \[\/code\]/is ', '/\[sub\ ' (. *?) \[\/sub\]/is ', '/\[sup\ ' (. *?) \[\/sup\]/is ', '/\[p\ ' (. *?) \[\/p\]/is ', "/\[youtube\" (. *?) \[\/youtube\]/i ","/\[youku\ "(. *?) \[\/youku\]/i ","/\[gvideo\ "(. *?) \[\/gvideo\]/i ",//" specials ", Xhtml-like BBC Repository '/\[bull \/\]/i ', '/\[copyright \/\]/i ', '/\[registered \/\]/i ' , '/\[tm \/\]/i ',);p rivate sTatic $simple _replace = Array (' <strong>$1</strong> ', ' <em>$1</em> ', ' <u>$1</u> ' ,//can also use: ' <span style= ' text-decoration:underline; >$1</span> ' <del>$1</del> ',//can also use: ' <span style= ' Text-decoration:line-through; ">$1</span>" <span style= "font-size: $;" >$2</span> ', ' <span style= ' color: $ $; " >$2</span> ', ' <div style= ' text-align:center; " >$1</div> ', ' <span style= ' font-family: $; " >$2</span> ', ' <div style= ' text-align: $; " >$2</div> ', ' <a href= ' $ ' >$1</a> ', ' <a href= ' $ ' >$2</a> ', ' <a href= ' mailto:$1 ' >$2</a> ', ' <a href= ' mailto:$1 ' >$1</a> ', ' ', ' ', ' "$1</blockquote> ',//can also use: ' <div>$1</div> ' <bloc kquote><strong>$1 wrote:</strong> $2</blockquote> ',//can also use: ' <div><strong& gt;$1 wrote:</strong> $2</div> ' <pre>$1</pre> ', ' <sub>$1</sub> ', ' <sup>$1 </sup> ', ' <p>$1</p> ', "<object width=\" 425\ "height=\" 350\ "><embed src=\"/http/ Www.youtube.com/v/$1\ "type=\" application/x-shockwave-flash\ "width=\" 425\ "height=\" 350\ "></embed></ Object> "," <object width=\ "425\" height=\ "350\" ><embed src=\ "http://player.youku.com/player.php/sid/$1 /v.swf\ "type=\" application/x-shockwave-flash\ "width=\" 425\ "height=\" 350\ "></embed></object>", " <embed src=\ "http://player.youku.com/player.php/sid/$1/v.swf\" type=\ "application/x-shockwave-flash\" style=\ " width:425px; Height:350px;\ ">",//"specials", Xhtml-like BBC Repository ' • ', ' © ', ' ® ', ' ™ ',);p rivate static $lineBreaks _search = Array (//[list]//'/\<br \/\>\s*\[list (. *?) \]/i ',//uncomment to remove <br/> Before the tag '/\[list (. *?) \](.+?) \[\/list\]/sie ', '/\[\/list\]\s*\<br \/\>/i ',//[code]//'/\<br \/\>\s*\[code\]/i ',//uncomment to REM Ove <br/> Before the tag '/\[code\] (. +?) \[\/code\]/sie ', '/\[\/code\]\s*\<br \/\>/i ',//[quote]//'/\<br \/\>\s*\[quote (. *?) \]/i ',//uncomment to remove <br/> Before the tag '/\[\/quote\]\s*\<br \/\>/i ',//[p]//'/\<br \/\>\s *\[p\]/i ',//uncomment to remove <br/> Before the tag '/\[\/p\]\s*\<br \/\>/i ',//[center]//'/\&L T;br \/\>\s*\[center\]/i ',//uncomment to remove <br/> Before the tag '/\[\/center\]\s*\<br \/\>/i ' ,//[align]//'/\<br \/\>\s*\[align (. *?) \]/i ',//uncomment to remove <br/> Before the tag '/\[\/align\]\s*\<br \/\>/i ',);p rivate static $lineBreak S_replace = Array (//[list]//] \n[list$] ",///uncomment to remove <br/> Before the tag" ' [list$1] '. Str_replace (' <br/> ', ', ' $ "). ' [/list] ' "," [/list] ",//[code]//" \n[code] ",//uncomment to remove <br/> Before the tag" ' [Code] '. Str_repla Ce (' <br/> ', ' ', ' $ '). ' [/code] ' "," [/code] ",//[quote]//" \n[quote$1] ",//uncomment to remove <br/> before the tag" [/quote] ",//[p]/ /"\n[p]",//uncomment to remove <br/> before the tag "[/P]",//[center]//"\n[center]",//Unco mment to remove <br/> before the tag "[/center]",//[align]//' \n[align$1] ',//uncomment to remove <br/&G T Before the tag "[/align]",);p rivate static function Process_list_items ($list _items) {$result _list_items = array ();// Check for [li][/li] Tagspreg_match_all ("/\[li\" (. *?) \[\/li\]/is ", $list _items, $li _array); $li _array = $li _array[1];if (empty ($li _array)) {//We didn ' t find any [Li] tags$list _items_array = Explode ("[*]", $list _items); foreach ($list _items_array as $Li_text) {$li _text = Trim ($li _text), if (Empty ($li _text)) {continue;} $li _text = nl2br ($li _text); $result _list_items[] = ' <li> '. $li _text. ' </li> ';}} else {//we found [Li] Tags!foreach ($li _array as $li _text) {$li _text = nl2br ($li _text); $result _list_items[] = ' <li> '. $li _text. ' </li> ';}} $list _items = implode ("\ n", $result _list_items); return $list _items;} /*arguments: $string = The text want to Format*//*it are recomended to do $string = Htmlentities ($string) before Callin G Parse*/public static function parse ($string) {$s = (string) $string; if (empty ($s)) {return ';} Preappend/HTTP to URL address if not present$s = Preg_replace ('/\[url\= ([^ (http)].+?) \](.*?) \[\/url\]/i ', ' [Url=http://$1]$2[/url] ', $s); $s = preg_replace ('/\[url\] ([^ (http)].+?) \[\/url\]/i ', ' [Url=http://$1]$1[/url] ', $s);//ADD Line breaks$s = NL2BR ($s);//Remove the Trash made by previous$s = Pre G_replace (self:: $lineBreaks _search, Self:: $lineBreaks _replace, $s);//Parse bbcode$s = Preg_replace (sElf:: $simple _search, Self:: $simple _replace, $s);//Parse [list] tags$s = Preg_replace ('/\[list\] (. *?) \[\/list\]/sie ', ' <ul>\n '. Self::p rocess_list_items ("$"). " \n</ul> "', $s); $s = preg_replace ('/\[list\= (disc|circle|square|decimal|decimal-leading-zero|lower-roman| upper-roman|lower-greek|lower-alpha|lower-latin|upper-alpha|upper-latin|hebrew|armenian|georgian| Cjk-ideographic|hiragana|katakana|hiragana-iroha|katakana-iroha|none) \] (. *?) \[\/list\]/sie ', ' "<ol style=\" List-style-type: $1;\ ">\n". Self::p rocess_list_items ("$"). " \n</ol> "', $s); return $s;}}? >
PHP Sample code:
<?php//http://www.codepearl.cominclude "BBCode.class.php"; $bbtext = <<<bbtext[b]bold[/b][i]italic[/i] [U]underline[/u][s]strike through[/s][size=25px]size 25px[/size][color=red]color red[/color][center]center text[/ Center][font=monospace]monospace[/font][align=right]align right[/align]links:[url]http://www.bbcode.org/[/url][ Url=http://www.bbcode.org/]linking to a named site[/url][url]www.bbcode.org/[/url][url=www.bbcode.org/]linking to a Named site (no HTTP) [/url][[email protected]]mail to You[/mail][mail][email protected][/mail][bull/][ Copyright/][registered/][tm/][img]http://www.bbcode.org/images/lubeck_small.jpg[/img]resized image:[img=100x50] Http://www.bbcode.org/images/lubeck_small.jpg[/img]clickable image:[url=http://www.bbcode.org/images/lubeck.jpg ][img]http://www.bbcode.org/images/lubeck_small.jpg[/img][/url]resized image with Meta information:[img width= "100" height= "alt=" Lubeck City Gate "Title=" The one of the medieval city gates of Lubeck "]htTP://WWW.BBCODE.ORG/IMAGES/LUBECK_SMALL.JPG[/IMG] [Quote]quote[/quote][quote=quote]quote[/quote][code] preformatted textpreformatted textpreformatted text[/code]unordered list[list][*]unordered Item 1[*]unordered Item 2[/ list]ordered list[list=decimal][*]item1[*]item2[/list]another Ordered list[list=upper-roman][li]list Item 1[/li][li ]list Item 2[/li][/list]normal text. [Sub]subscript[/sub]normal text. [Sup]superscript[/sup][p]paragraph[/p]youku Video[youku]xmtu2nta3mdaxmg==[/youku]good Video[gvideo] Xmtu2nta3mdaxmg==[/gvideo]bbtext;echo BBCode::p arse ($bbtext);//the output is:/*<strong>bold</strong> <BR/><em>italic</em><br/><u>underline</u><br/><del>strike THROUGH</DEL><BR/><span style= "font-size:25px;" >size 25px</span><br/><span style= "color:red;" >color red</span><br/><div style= "Text-align:center;" >center text</div><span style= "font-family:monospace;" >mONOSPACE</SPAN><BR/><div style= "text-align:right;" >align right</div>links:<br/><a href= "http://www.bbcode.org/" >http://www.bbcode.org/</a ><BR/><a href= "http://www.bbcode.org/" >linking to a named site</a><br/><a href= "http:/ /www.bbcode.org/">www.bbcode.org/</a><br/><a href=" http://www.bbcode.org/">linking to a named Site (no HTTP) </a><br/><a href= "mailto:[email protected]" >mail to you</a><br/> <a href= "mailto:[email protected]" >[email protected]</a><br/><br/>·©®™<br/ ><BR/><br/>resized image:<br/ ><br/>clickable IMAGE:<BR/><a href= "http://www.bbcode.org/images/lubeck.jpg" ></a><br/>resized image with Meta information:<br/><br/><br/><blockquote>quote</blockquote><blockquote><strong>quote wrote:</strong> quote</blockquote><pre>preformatted textpreformatted textpreformatted text</ PRE><BR/>unordered list<br/><ul><li>unordered Item 1</li><li>unordered Item 2</LI></UL><BR/>ordered list<br/><ol style= "list-style-type:decimal;" ><li>item1</li><li>item2</li></ol><br/>another ordered list<br/> <ol style= "List-style-type:upper-roman;" ><li>list Item 1</li><li>list Item 2</li></ol><br/>normal text. <SUB>SUBSCRIPT</SUB><BR/>normal Text. <SUP>SUPERSCRIPT</SUP><BR/><p>paragraph</p>youtube video<br/><object Width= "425" height= "><embed src=" Http://www.youtube.com/v/uelHwf8o7_U "type=" application/ X-shockwave-flash "width=" 425 "height=", ></embed></object><br/>google video<br/> <embed src= "http://video.google.com/googleplayer.swf?docId=3425879595449788861" type= "application/ X-shockwave-flash "style=" width:425px; height:350px; " ><br/><br/>*/?>
Discuz Code to HTML code