<?php
function TMPL_Parser_BB( $tmpl )
{
$argv = func_get_args();
$tmpl = preg_replace( "#\[b\](.+?)\[/b\]#is" , "<b>\\1</b>" , $tmpl );
$tmpl = preg_replace( "#\[i\](.+?)\[/i\]#is" , "<i>\\1</i>" , $tmpl );
$tmpl = preg_replace( "#\[u\](.+?)\[/u\]#is" , "<u>\\1</u>" , $tmpl );
$tmpl = preg_replace( "#\[center\](.+?)\[/center\]#is" , "<div align='center'>\\1</div>" , $tmpl );
$tmpl = preg_replace( "#\[right\](.+?)\[/right\]#is" , "<div align='right'>\\1</div>" , $tmpl );
$tmpl = preg_replace( "#\[left\](.+?)\[/left\]#is" , "<div align='left'>\\1</div>" , $tmpl );
$tmpl = preg_replace( "#<div align='(center|right|left)'>(.+?)</div>[^</p>]#is", "<div align='\\1'>\\2</div><br>" , $tmpl );
$tmpl = preg_replace( "#\(c\)#i" , "©" , $tmpl );
$tmpl = preg_replace( "#\(tm\)#i" , "(tm)" , $tmpl );
$tmpl = preg_replace( "#\(r\)#i" , "®" , $tmpl );
while ( preg_match( "#\[font=([^\]]+)\](.*?)\[/font\]#ies", $tmpl ) )
{ $tmpl = preg_replace( "#\[font=([^\]]+)\](.*?)\[/font\]#is" , $this->regex_font_attr(array('s'=>'font','1'=>'\\1','2'=>'\\2')), $txt ); }
while( preg_match( "#\[color=([^\]]+)\](.+?)\[/color\]#ies", $tmpl ) )
{ $tmpl = preg_replace( "#\[color=([^\]]+)\](.+?)\[/color\]#is" , "<span style='color:\\1'>\\2</span>" , $tmpl ); }
if( isset( $argv[2] ) && is_array( $argv[2] ) )
{
$tmpl = preg_replace( "#\[img=(\d)\]#ie" ,
$this -> TMPL_Parser_BB_Image( "\\1" , "" , $argv[1] , $argv[2] ) ,
$tmpl );
$tmpl = preg_replace( "#\[img=(\d);align=(\w+)\]#ie" ,
$this -> TMPL_Parser_BB_Image("\\1" , "\\2" , $argv[1] , $argv[2] ) ,
$tmpl );
}
$tmpl = preg_replace( "#\[img\](.+?)\[/img\]#ie" , "<img src='1'>" , $tmpl );
$tmpl = preg_replace( "#\[url\](.+?)\[/url\]#ie" ,
"TMPL_Parser_BB_link( \"\\1\" , \"\" )" ,
$tmpl );
$tmpl = preg_replace( "#\[url\s*=\s*(\S+?)\s*\](.*?)\[\/url\]#ie" ,
"TMPL_Parser_BB_link( \"\\1\" , \"\\2\" )" ,
$tmpl );
$tmpl = preg_replace( "#\n?\[list\](.+?)\[/list\]\n?#ies" ,
"TMPL_Parser_BB_CODE_List( \"\\1\" , \"\" )" ,
$tmpl );
$tmpl = preg_replace( "#\n?\[list=(a|A|i|I|1)\](.+?)\[/list=(a|A|i|I|1)\]\n?#ies" ,
"TMPL_Parser_BB_CODE_List( \"\\2\" , \"\\1\" )" ,
$tmpl );
$tmpl = str_replace( "<p></ul>" , "</ul><p>" , $tmpl );
$tmpl = str_replace( "<p><li>" , "<li>" , $tmpl );
$tmpl = str_replace( "</ul><li>" , "</ul><p><li>" , $tmpl );
$tmpl = @preg_replace_callback( "#\[html\](.+?)\[/html\]#is", array( $this , "TMPL_Parser_BB_CODE_HTML" ) , $tmpl );
$tmpl = TMPL_Parser_BB_Remove( $tmpl );
return $tmpl;
}
function TMPL_Parser_BB_Remove( $tmpl )
{
$tmpl = preg_replace( "#\[b\]|\[/b\]#is" , "" , $tmpl );
$tmpl = preg_replace( "#\[i\]|\[/i\]#is" , "" , $tmpl );
$tmpl = preg_replace( "#\[u\]|\[/u\]#is" , "" , $tmpl );
$tmpl = preg_replace( "#\[center\]|\[/center\]#is" , "" , $tmpl );
$tmpl = preg_replace( "#\[right\]|\[/right\]#is" , "" , $tmpl );
$tmpl = preg_replace( "#\[left\]|\[/left\]#is" , "" , $tmpl );
$tmpl = preg_replace( "#\[font\s*=\s*(\S+?)\s*\]\[\/font\]#is" , "" , $tmpl );
$tmpl = preg_replace( "#\[color\s*=\s*(\S+?)\s*\]|\[\/color\]#is" , "" , $tmpl );
$tmpl = preg_replace( "#\[img\]|\[/img\]#is" , "" , $tmpl );
$tmpl = preg_replace( "#\[img=\d\]#is" , "" , $tmpl );
$tmpl = preg_replace( "#\[img=\d;align=(.*?)\]#is" , "" , $tmpl );
$tmpl = preg_replace( "#\[url\]|\[/url\]#is" , "" , $tmpl );
$tmpl = preg_replace( "#\[url\s*=\s*(\S+?)\s*\]#is" , "" , $tmpl );
$tmpl = preg_replace( "#\[html\]|\[/html\]#is" , "" , $tmpl );
return $tmpl;
}
function TMPL_Parser_BB_Image( $number , $align , $src , $images = array() )
{
$align = ( empty( $align ) ) ? "align='left'" : "align='" . $align . "'" ;
if( isset( $images[ $number - 1 ] ) )
{ return "<img src='images/" . $src . $images[ $number - 1 ] . "' border='0' ". $align .">"; }
else
{ return "[Image Not Found]"; }
}
function TMPL_Parser_BB_link( $href , $discription )
{
$style = "content";
if( empty( $discription ) )
{
$discription = ( strlen( $discription ) < 80 ) ? $href : substr( $href , 0 , 80 ) . "..." ;
return "<a href=" . $href . " class='" . $style . "'>" . $discription . "</a>";
}
else
{ return "<a href=" . $href . " class='" . $style . "'>" . $discription . "</a>"; }
}
function TMPL_Parser_BB_CODE_List( $tmpl , $type )
{
switch( $type )
{
case "1": $list_type = "1"; break;
case "A": $list_type = "A"; break;
case "a": $list_type = "a"; break;
case "I": $list_type = "I"; break;
case "i": $list_type = "i"; break;
default: $list_type = " "; break;
}
$tmpl = preg_replace( "#\[\*\]#" , "<li>" , $tmpl );
$tmpl = "<ul type='" . $list_type . "'>" . $tmpl . "</ul>";
return $tmpl;
}
//print TMPL_Parser_BB( $text );
//Для замены картинок необходимо передать:
//вторым параметром: путь от корня до папки с картинками
// третьим - имена файлов самих картинок (массив)
?>