Posts: 251
Location: Belgium, Antwerp, Zoersel
|
Of course there is, you just need a BBC and smiley parser. I made both for my sites, here they are:
BBC class:
PHP Code:
<?php
/* version 1.1 */
class bbc{
var $error_quote;
var $open_quote;
var $close_quote;
var $bbc;
var $html;
function bbc(){
global $settings;
$this->bbc = array(
'url1' => "#\[url\](\S+?)\[/url\]#Si",
'url2' => "#\[url=(https?|ftp|email)://(\S+?)\](.*?)\[/url\]#Si",
'url3' => "#\[url=/?(\S+?)\](.*?)\[/url\]#Si",
'email1' => "#\[email\]([\.\w\-]+)(@|%40|&\#064;)([\.\w\-]+\.[\w\-]+)\[/email\]#Si",
'email2' => "#\[email=([\.\w\-]+)(\@|%40|&\#064;)([\.\w\-]+\.[\w\-]+)\](.*?)\[/email\]#Si",
'img1' => "#\[img\](.*?)\[/img\]#Si",
'img2' => "#\[img:([0-9]+),([0-9]+)\](.*?)\[/img\]#Si",
'b' => "#\[b\](.*?)\[/b\]#Ssi",
'u' => "#\[u\](.*?)\[/u\]#Ssi",
'i' => "#\[i\](.*?)\[/i\]#Ssi",
's' => "#\[s\](.*?)\[/s\]#Ssi",
'font' => "#\[font=(.*?)\](.*?)\[/font\]#Ssi",
'size' => "#\[size=(.*?)\](.*?)\[/size\]#Ssi",
'color' => "#\[color=(.*?)\](.*?)\[/color\]#Ssi",
'align' => "#\[align=(.*?)\](.*?)\[/align\]#Ssi",
'glow' => "#\[glow=(.*?),(.*?)\](.*?)\[/glow\]#Ssi",
'shadow' => "#\[shadow=(.*?),(.*?)\](.*?)\[/shadow\]#Ssi",
'blur' => "#\[blur=(.*?),(.*?)\](.*?)\[/blur\]#Ssi",
'ul_start' => '#\\[ul( .*?)?\\]#Si',
'ul_end' => '#\\[/ul\\]#Si',
'li_start' => '#\\[li( .*?)?\\]#Si',
'li_end' => '#\\[/li\\]#Si',
);
$this->html = array(
'url1' => '<a href="\\1" title="\\1">\\1</a>',
'url2' => '<a href="\\1://\\2" title="\\1://\\2">\\3</a>',
'url3' => '<a href="'.$settings['root']['url'].'/\\1" title="\\1">\\2</a>',
'email1' => '<a href="mailto:\\1@\\3" title="\\1@\\3">\\1@\\3</a>',
'email2' => '<a href="mailto:\\1@\\3" title="\\4">\\4</a>',
'img1' => '<img src="/getimage.php||url=\\1" alt=""/>',
'img2' => '<img src="/getimage.php||url=\\3|width=\\1|height=\\2" alt=""/>',
'b' => '<b>\\1</b>',
'u' => '<u>\\1</u>',
'i' => '<i>\\1</i>',
's' => '<s>\\1</s>',
'font' => '<span style="font-face:\\1;">\\2</span>',
'size' => '<span style="font-size:\\1;">\\2</span>',
'color' => '<span style="color:\\1;">\\2</span>',
'align' => '<div align="\\1">\\2</div>',
'glow' => '<div style="width: 100%; filter: glow(color=\\1,strength=\\2);">\\3</div>',
'shadow' => '<div style="width: 100%; filter: shadow(color=\\1,direction=\\2);">\\3</div>',
'blur' => '<div style="width: 100%; filter: blur(add=true,direction=\\1,strength=\\2);">\\3</div>',
'ul_start' => '<ul\\1>',
'ul_end' => '</ul>',
'li_start' => '<li\\1>',
'li_end' => '</li>',
);
}
function bbcode($string){
$str = preg_replace($this->bbc, $this->html, $string);
//parse quotes
//laten we aan een andere functie over
$str = preg_replace("#(\[quote\].*\[/quote\])#Sies", "\$this->parse_quote('\\1')", $str);
return stripslashes($str);
}
function parse_quote($to_quote=""){
if($to_quote == ""){
return;
}
$txt = $to_quote;
//replace open tags en close tags
$txt = preg_replace("#\[quote\]#ie", "\$this->quote_open()", $txt);
$txt = preg_replace("#\[/quote\]#ie", "\$this->quote_close()", $txt);
//alleen aangepaste string returnen als alles goed ging
if ( ($this->open_quote == $this->close_quote) && ($this->error_quote == 0) )
{
return trim($txt);
}
else
{
return $to_quote;
}
}
function quote_open()
{
//parse de quote open tag
$this->open_quote++;
return '<table style="border: 1px solid #000000; font-family: verdana; font-size: 8pt; color: #000000;" bgcolor="#DDDDDD" cellspacing="2" cellpadding="8" class="alt2"><tr><td><b>QUOTE:</b><hr noshade="noshade" color="#000000" />';
}
function quote_close()
{
//parse the quote close tag
if ( $this->open_quote == 0 )
{
$this->error_quote++;
return;
}
$this->close_quote++;
return '</td></tr></table>';
}
}
?>
String class (for smilies, line breaks and other clening up, depends on the BBC class):
PHP Code:
<?php
// version 1.1
require_once 'classes/bbc.php';
class string{
var $bbc;
var $newlines;
function string($newlines=true, $bbc=false){
if($bbc!==false)
$this->bbc=&$_GLOBALS[$bbc];
else
$this->bbc=new bbc;
$this->newlines=$newlines;
}
function remove_special_chars($str, $newlines=NULL){
//newlines?
if(is_null($newlines))
$newlines=$this->newlines;
//encode the string in the right way
$trans=get_html_translation_table(HTML_ENTITIES);
$str=strtr($str, $trans);
$chrs=array(
chr(150), chr(8211), //en dash
chr(151), chr(8212), //em dash
chr(147), chr(8220), chr(148), chr(8221), //double quote
chr(146), chr(8217), chr(145), chr(8216), // single quote
chr(169), //copyright mark
chr(174), //registration mark
chr(153), chr(8482), //trademark
chr(149), chr(8226), //bullet
chr(8226), //ellipses
chr(133), //double prime
'<', //back to html <
'>', //back to html >
'"', //back to html "
'&' //back to html &
);
$repl=array(
'', '',
'', '',
'"', '"', '"', '"',
''', ''', ''', ''',
'©',
'®',
'', '',
'', '',
'
',
'″',
'<',
'>',
'"',
'&'
);
$str=str_replace($chrs, $repl, $str);
//replace newlines if asked
if($newlines==true){
$str=preg_replace('#(ul|li)]\\s*\\n\\s*#si', '\\1] ', $str);
$str=nl2br($str);
}
//correct linebreaks
$str=preg_replace('#\s*<br\s*/?>\s*#i', '<br/>', $str);
return $str;
}
function replace_var($varname){
$indexes=strstr($varname, '[');
$varname=preg_replace('#\\[(.*?)\\]#', '', $varname);
eval("global \$$varname;");
eval("\$var=\$$varname$indexes;");
return $var;
}
function parse($str, $newlines=NULL){
//newlines?
if(is_null($newlines))
$newlines=$this->newlines;
//remove special chars
$str=$this->remove_special_chars($str, $newlines);
//parse variables
$str=preg_replace('#{\\$(.+?)}#Sie', "\$this->replace_var('\\1')", $str);
//parse The Smiley Site smilies
$str=preg_replace(
'#tss\\((.+?)\\)#Si',
'<a href="http://thesmileysite.com/smiley/\\1">'.
'<img src="http://thesmileysite.com/smileyimg/\\1" alt="\\1" border="0" />'.
'</a>',
$str
);
/*// Other smilies example, not in my original file
$str=str_replace(
array(';)', ':D', ':('),
array(
'<img src="/winksmiley.gif" alt="wink" />',
'<img src="/veryhappy.gif" alt="very happy" />',
'<img src="/sad.gif" alt="sad" />',
),
$str
);
*/
//parse bbc code
$str=$this->bbc->bbcode($str);
return $str;
}
}
?>
|