Zend - The PHP Company




Miscellaneous

Add Code


BBCode Validation  

Type: code fragment
Added by: akmalxxx
Entered: 18/10/2006
Last modified: 02/11/2005
Rating: - (fewer than 3 votes)
Views: 2142
Simple bbcode validating function


<?
//BBCode handler

$bbend=array();

//replacement array
$smilie_url=URL.'img/bbcode/smilies/';
$bb_stag=array(
    
'[*]',
    
'[b]','[/b]',
    
'[i]','[/i]',
    
'[u]','[/u]',
    
'[/original]','[/align]','[/list]','[/nlist]','[/quote]','[/code]','[/color]','[/size]',
    
':lol:',':wink:',':roll:',':oops:',':idea:',
    
':|',':)',':(',':o',':x','8)',':?'
);
$bb_srep=array(
    
'<li>',
    
'<strong>','</strong>',
    
'<em>','</em>',
    
'<span style="text-decoration:underline">','</span>',
    
'</div>','</div>','</ul>','</ol>','</div>','</div>','</span>','</span>',
    
'<img src="'.$smilie_url.'neutral.gif" alt="Neutral"/>',
    
'<img src="'.$smilie_url.'smile.gif" alt="Smile"/>',
    
'<img src="'.$smilie_url.'sad.gif" alt="Sad"/>',
    
'<img src="'.$smilie_url.'surprised.gif" alt="Surprised"/>',
    
'<img src="'.$smilie_url.'mad.gif" alt="Mad"/>',
    
'<img src="'.$smilie_url.'cool.gif" alt="Cool"/>',
    
'<img src="'.$smilie_url.'confused.gif" alt="Confused"/>',
    
'<img src="'.$smilie_url.'lol.gif" alt="Laughing"/>',
    
'<img src="'.$smilie_url.'wink.gif" alt="Wink"/>',
    
'<img src="'.$smilie_url.'rolleyes.gif" alt="Roll eyes"/>',
    
'<img src="'.$smilie_url.'redface.gif" alt="Red face"/>',
    
'<img src="'.$smilie_url.'idea.gif" alt="Idea"/>'
);

$bb_ptag=array(
    
'/[img](.+?)[/img]/s',
    
'/[url](.+?)[/url]/s',
    
'/[url=(.+?)](.+?)[/url]/s',
    
'/[align=(left|center|right|justify)]/',
    
'/[quote]/',
    
'/[quote=(.+?)]/',
    
'/[code]/',
    
'/[original=(.+?)]/',
    
'/[color=(.+?)]/',
    
'/[size=(.+?)]/',
    
'/[list]/',
    
'/[nlist]/'
);
$bb_prep=array(
    
'<img src="$1" alt="Image"/>',
    
'<a href="$1">$1</a>',
    
'<a href="$1">$2</a>',
    
'<div align="$1">',
    
'<div class="quote"><div class="quote_t">Quote</div>',
    
'<div class="quote"><div class="quote_t">Quote <span class="quote_st">$1</span></div>',
    
'<div class="code"><div class="code_t">Code</div>',
    
'<div class="ori"><div class="ori_t">Original message from $1</div>',
    
'<span style="color:$1">',
    
'<span style="font-size:$1%">',
    
'<ul>',
    
'<ol>'
);


function 
bbcoder_safe($str)
{
    global 
$bbend;
    
$bbend=array(); //reset
    
$str=trim($str); //clear spaces

    //valid start end tag
    
$buf=preg_replace_callback("/[(.*?)]/",'bbcoder_close',$str);
    
$c=$p='';
    while (
$p=array_pop($bbend)) $c.='[/'.$p.']';
    
$buf.=$c;
    return 
$buf;
}

function 
bbcoder_close($tag)
{
    global 
$bbend;
    
$stok=array('*');

    
$t=explode('=',$tag[1]);
    
$n=strtolower(trim($t[0])); //tag
    
$p=strtolower(trim($t[1])); //param
    
if ($p$p='='.$p;

    
//single tag    
    
if (in_array($n,$stok)) return '['.$n.$p.']';

    
//block tag
    
if ($n[0]!='/') { array_push($bbend,$n); return '['.$n.$p.']'; } //open
    
else { //close
        
$n=ltrim($n,'/');
        if (
in_array($n,$bbend)) {
            
$c=$t='';
            while (
$t!=$n) { $t=array_pop($bbend); $c.='[/'.$t.']'; }
            return 
$c;
        }
        else return 
'[/'.$n.']';
    }
    return 
'';
}

function 
bbcoder_parse($str)
{
    global 
$bb_stag,$bb_srep,$bb_ptag,$bb_prep;

    
$str=nl2br(htmlentities($str));
    
$str=str_replace($bb_stag,$bb_srep,$str);
    
$str preg_replace($bb_ptag,$bb_prep,$str);
    return 
$str;
}
?>


Usage Example


user bbcoder_safe to validate and bbcoder_parse to display html version


Rate This Script





Search



This Category All Categories