Este es la clase:
bbcode.inc.php
Código PHP:
<?php
function begtoend($htmltag){
return preg_replace('/<([A-Za-z]+)>/','</$1>',$htmltag);
}
function replace_pcre_array($text,$array){
$pattern = array_keys($array);
$replace = array_values($array);
$text = preg_replace($pattern,$replace,$text);
return $text;
}
class bbcode{
var $tags;
var $settings;
function bbcode(){
$this->tags = array();
$this->settings = array('enced'=>true);
}
function get_data($name,$cfa = ''){
if(!array_key_exists($name,$this->tags)) return '';
$data = $this->tags[$name];
if($cfa) $sbc = $cfa; else $sbc = $name;
if(!is_array($data)){
$data = preg_replace('/^ALIAS(.+)$/','$1',$data);
return $this->get_data($data,$sbc);
}else{
$data['Name'] = $sbc;
return $data;
}
}
function change_setting($name,$value){
$this->settings[$name] = $value;
}
function add_alias($name,$aliasof){
if(!array_key_exists($aliasof,$this->tags) or array_key_exists($name,$this->tags)) return false;
$this->tags[$name] = 'ALIAS'.$aliasof;
return true;
}
function onparam($param,$regexarray){
$param = replace_pcre_array($param,$regexarray);
if(!$this->settings['enced']){
$param = htmlentities($param);
}
return $param;
}
function export_definition(){
return serialize($this->tags);
}
function import_definiton($definition,$mode = 'append'){
switch($mode){
case 'append':
$array = unserialize($definition);
$this->tags = $array + $this->tags;
break;
case 'prepend':
$array = unserialize($definition);
$this->tags = $this->tags + $array;
break;
case 'overwrite':
$this->tags = unserialize($definition);
break;
default:
return false;
}
return true;
}
function add_tag($params){
if(!is_array($params)) return 'Paramater array not an array.';
if(!array_key_exists('Name',$params) or empty($params['Name'])) return 'Name parameter is required.';
if(preg_match('/[^A-Za-z]/',$params['Name'])) return 'Name can only contain letters.';
if(!array_key_exists('HasParam',$params)) $params['HasParam'] = false;
if(!array_key_exists('HtmlBegin',$params)) return 'HtmlBegin paremater not specified!';
if(!array_key_exists('HtmlEnd',$params)){
if(preg_match('/^(<[A-Za-z]>)+$/',$params['HtmlBegin'])){
$params['HtmlEnd'] = begtoend($params['HtmlBegin']);
}else{
return 'You didn\'t specify the HtmlEnd parameter, and your HtmlBegin parameter is too complex to change to an HtmlEnd parameter. Please specify HtmlEnd.';
}
}
if(!array_key_exists('ParamRegexReplace',$params)) $params['ParamRegexReplace'] = array();
if(!array_key_exists('ParamRegex',$params)) $params['ParamRegex'] = '[^\\]]+';
if(!array_key_exists('HasEnd',$params)) $params['HasEnd'] = true;
if(array_key_exists($params['Name'],$this->tags)) return 'The name you specified is already in use.';
$this->tags[$params['Name']] = $params;
return '';
}
function parse_bbcode($text){
foreach($this->tags as $tagname => $tagdata){
if(!is_array($tagdata)) $tagdata = $this->get_data($tagname);
$startfind = "/\\[{$tagdata['Name']}";
if($tagdata['HasParam']){
$startfind.= '=('.$tagdata['ParamRegex'].')';
}
$startfind.= '\\]/';
if($tagdata['HasEnd']){
$endfind = "[/{$tagdata['Name']}]";
$starttags = preg_match_all($startfind,$text,$ignore);
$endtags = substr_count($text,$endfind);
if($endtags < $starttags){
$text.= str_repeat($endfind,$starttags - $endtags);
}
$text = str_replace($endfind,$tagdata['HtmlEnd'],$text);
}
$replace = str_replace(array('%%P%%','%%p%%'),'\'.$this->onparam(\'$1\',$tagdata[\'ParamRegexReplace\']).\'','\''.$tagdata['HtmlBegin'].'\'');
$text = preg_replace($startfind.'e',$replace,$text);
}
return $text;
}
}
?>
Código PHP:
# Smilies
$bbcode = str_replace(":molesto:","<img src='images/smilies/smile0000.gif' alt='Molesto' align='absmiddle' border='0' class='hand'>", $bbcode);
$bbcode = str_replace(":D","<img src='images/smilies/smile0001.gif' alt='Felicidad' align='absmiddle' border='0' class='hand'>", $bbcode);
$bbcode = str_replace(":asombrado:","<img src='images/smilies/smile0002.gif' alt='Asombrado' align='absmiddle' border='0' class='hand'>", $bbcode);
$bbcode = str_replace(":hummmm:","<img src='images/smilies/smile0003.gif' alt='Hummmm' align='absmiddle' border='0' class='hand'>", $bbcode);
$bbcode = str_replace("8)","<img src='images/smilies/smile0004.gif' alt='Llevo gafas' align='absmiddle' border='0' class='hand'>", $bbcode);
$bbcode = str_replace("8(","<img src='images/smilies/smile0005.gif' alt='Triste' align='absmiddle' border='0' class='hand'>", $bbcode);
$bbcode = str_replace(":sonriente:","<img src='images/smilies/smile0006.gif' alt='Contento' align='absmiddle' border='0' class='hand'>", $bbcode);
$bbcode = str_replace(":sonrisaalados:","<img src='images/smilies/smile0007.gif' alt='Sonriente a los lados' align='absmiddle' border='0' class='hand'>", $bbcode);
$bbcode = str_replace(":(","<img src='images/smilies/smile0008.gif' alt='Acongojado' align='absmiddle' border='0' class='hand'>", $bbcode);
$bbcode = str_replace(":contento:","<img src='images/smilies/smile0009.gif' alt='Sonriente' align='absmiddle' border='0' class='hand'>", $bbcode);
$bbcode = str_replace(":jajaja:","<img src='images/smilies/smile0011.gif' alt='Risotada' align='absmiddle' border='0' class='hand'>", $bbcode);
$bbcode = str_replace(":sonrojado:","<img src='images/smilies/smile0012.gif' alt='Me pongo rojo' align='absmiddle' border='0' class='hand'>", $bbcode);
$bbcode = str_replace(":muymolesto:","<img src='images/smilies/smile0013.gif' alt='Estoy muy molesto' align='absmiddle' border='0' class='hand'>", $bbcode);
$bbcode = str_replace(":arghhhh:","<img src='images/smilies/smile0014.gif' alt='Asustado' align='absmiddle' border='0' class='hand'>", $bbcode);
$bbcode = str_replace(":pensando:","<img src='images/smilies/smile0015.gif' alt='Pensativo' align='absmiddle' border='0' class='hand'>", $bbcode);
$bbcode = str_replace(":jejeje:","<img src='images/smilies/smile0017.gif' alt='Risita' align='absmiddle' border='0' class='hand'>", $bbcode);
$bbcode = str_replace(":picaro:","<img src='images/smilies/smile0018.gif' alt='Picaro que eres' align='absmiddle' border='0' class='hand'>", $bbcode);
$bbcode = str_replace(":b","<img src='images/smilies/smile0019.gif' alt='Sacar la lengua' align='absmiddle' border='0' class='hand'>", $bbcode);
$bbcode = str_replace(":star:","<img src='images/smilies/smile0029.gif' alt='Enviar carta' width='19' height='19' border='0' class='hand'>", $bbcode);
$bbcode = str_replace(":mail:","<img src='images/smilies/smile0036.gif' alt='Estrella' align='absmiddle' border='0' class='hand'>", $bbcode);
$bbcode = str_replace(":corazon:","<img src='images/smilies/smile0026.gif' alt='Corazón' align='absmiddle' border='0' class='hand'>", $bbcode);
$bbcode = str_replace(":corazonroto:","<img src='images/smilies/smile0027.gif' alt='Corazón roto' align='absmiddle' border='0' class='hand'>", $bbcode);
$bbcode = str_replace(":ideas:","<img src='images/smilies/smile0028.gif' alt='Ideas y más ideas' align='absmiddle' border='0' class='hand'>", $bbcode);
$bbcode = str_replace(":mirandoizquierda:","<img src='images/smilies/smile0031.gif' alt='Mirando a la izquierda' align='absmiddle' border='0' class='hand'>", $bbcode);
$bbcode = str_replace(":mirandoderecha:","<img src='images/smilies/smile0032.gif' alt='Mirando a la derecha' align='absmiddle' border='0' class='hand'>", $bbcode);
$bbcode = str_replace(":dedosarriba:","<img src='images/smilies/smile0037.gif' alt='Sí;' align='absmiddle' border='0' class='hand'>", $bbcode);
$bbcode = str_replace(":dedosabajo:","<img src='images/smilies/smile0038.gif' alt='No' align='absmiddle' border='0' class='hand'>", $bbcode);
$bbcode = str_replace(":dedosxzados:","<img src='images/smilies/smile0039.gif' alt='Dedos cruzados' align='absmiddle' border='0' class='hand'>", $bbcode);
# Texto
preg_match_all ("/(\[)(php)(])(.*)(\[\/php\])/iU", $text, $l);
for($i=0; $i< count($l[0]); $i++){
$bbcode = str_replace($l[0][$i],PHPFormat($l[4][$i]),$text);
}
// Funcion para PHP
function PHPFormat($php) {
$php = str_replace("<br >","\n",$php);
ob_start();
highlight_string($php);
$cfinal = ob_get_contents();
ob_end_clean();
$keycol = ini_get("highlight.keyword");
$manual = "http://www.php.net/manual-lookup.php?lang=es&pattern=";
$cfinal = preg_replace('{([\w_]+)(\s*</font>)'.
'(\s*<font\s+color="'.$keycol.'">\s*\()}m',
'<a href="'.$manual.'$1" title="Ayuda en [url]www.php.net[/url] sobre $1" target="_blank">$1</a>$2$3',
$cfinal);
return "<div class='ccs_title'>Código PHP:<div class='css'>".$cfinal."</div></div>";
}