<?PHP /** * Basic BBCode parsing */ class BBcode{ public static $bbtags = array( '[h1]' => '<h1>','[/h1]' => '</h1>', '[h2]' => '<h2>','[/h2]' => '</h2>', '[h3]' => '<h3>','[/h3]' => '</h3>', '[table]' => '<table>','[/table]' => '</table>', '[tr]' => '<tr>','[/tr]' => '</tr>', '[td]' => '<td>','[/td]' => '</td>', '[p]' => '<p>','[/p]' => '</p>', '[left]' => '<div style="text-align:left;">','[/left]' => '</div>', '[right]' => '<div style="text-align:right;">','[/right]' => '</div>', '[center]' => '<div style="text-align:center;">','[/center]' => '</div>', '[justify]' => '<div style="text-align:justify;">','[/justify]' => '</div>', '[b]' => '<b>','[/b]' => '</b>', '[i]' => '<i>','[/i]' => '</i>', '[u]' => '<u>','[/u]' => '</u>', '[ol]' => '<ol>','[/ol]' => '</ol>', '[ul]' => '<ul>','[/ul]' => '</ul>', '[li]' => '<li>','[/li]' => '</li>', '[br]' => '<br>', '[pre]' => '<pre>','[/pre]' => '</pre>', '[/class]' => '</div>', ); public static $bbextended = array( //"/\n[\s| ]*\r/"=>"<br>", "/\[url](.*?)\[\/url]/i" => "<a href=\"http://$1\" title=\"$1\">$1</a>", "/\[url=(.*?)\](.*?)\[\/url\]/i" => "<a href=\"$1\" title=\"$2\">$2</a>", "/\[img\]([^[]*)\[\/img\]/i" => "<img src=\"$1\" alt=\" \" />", "/\[img=(.*?)\](.*?)\[\/img\]/i" =>"<img src=\"$1\" alt=\"$2\" title=\"$2\" />", "/\[color=(.*?)\](.*?)\[\/color\]/i" => "<font color=\"$1\" >$2</font>", "/\[class=(.*?)\]/" => "<div class=\"$1\">", //"/\[img=(.*?)\](.*?)\[\/img\]/i" => "<img src=\"$1\" class=\"$2\" />", ); public static function toHtml($bbtext){ $bbtext = Html::chars($bbtext); $bbtext = str_ireplace(array_keys(self::$bbtags), array_values(self::$bbtags), $bbtext); foreach(self::$bbextended as $match=>$replacement){ $bbtext = preg_replace($match, $replacement, $bbtext); } return $bbtext; } }