here's my bbcode parser class : http://runnable.com/VE929Bgd9lRotkWp/bbcode-for-php
<?php
class BBCode {
public function __construct(){}
private function showBBcodes($text) {
$text = htmlspecialchars($text); //skip HTML
preg_match_all('#\[code\](.*?)\[/code]#is', $text, $stack);
// BBcode array
$find = array(
'~\[b\](.*?)\[/b\]~s',
'~\[i\](.*?)\[/i\]~s',
'~\[quote\](.*?)\[/quote\]~s',
//'~\[code\](.*?)\[/code\]~s'
);
// HTML tags to replace BBcode
$replace = array(
'<b>$1</b>',
'<i>$1</i>',
'</p><blockquote>$1</blockquote></p>',
//'<div class="code_box">$1</div>'
);
$text = nl2br(preg_replace($find,$replace,$text));
foreach($stack[1] as $t) {
$text = preg_replace('#\[code\].*?\[/code]#is', $t, $text,1);
}
return $text;
}
// Smiley to image
private function parseSmiley($text){
// Smiley to image
$smileys = array(
':wave:' => 'wave.gif',
':hahaha:' => 'hahaha.gif',
':help:' => 'help.gif'
);
// Now you need find and replace
foreach($smileys as $smiley => $img){
$text = str_replace(
$smiley,
"<img src='images/smiley/{$img}' alt='{$smiley}'/>",
$text
);
}
// Now only return it
return $text;
}
public function parser($message){
$parser_content = $message;
$parser_content = $this->showBBcodes($parser_content);
$parser_content = $this->parseSmiley($parser_content);
return $parser_content;
}
}
this class can BBcode parse & Smiley to image & auto link.
but, I have some question and I can't fix it.
if active Line 16 and 25
//'~\[code\](.*?)\[/code\]~s' //'<div class="code_box">$1</div>'
will be parser [code] [b]text[/b] [/code] tag content to storng
if not active Line 31 and 54.... I can't styling its CSS
Now existing code just can noparse BBcode (can't skip smily code), everybody can suggest more smart code to skip all content... ?