<?php
/*************************************************
bbtags.php (and tagreplacer) - simple way to use tags like [b]bold[/b] or [url=http://www.somesite.com]my site[/url]
in message boards, guestbooks etc on your own website
home page: www.samscripts.com/scripts/bbtags
support: support.samscripts.com/index.php
licence: freeware / optionally giftware ( www.samscripts.com/donate.php )
author: Sam Yapp
date: 8th September 2002
documentation and examples at www.samscripts.com
*************************************************/
class tagreplacer{
var $simpletags = array();
var $callbacks = array();
var $regextags = array();
var $replacetags = array();
var $simpletagscount = 0;
function addreplace($args){
if( is_array($args)){
$this->changingtags = array_merge($this->changingtags, $args);
}else{
$this->changingtags[] = $args;
}
}
function addtags($args){
if( is_array($args) ){
$this->simpletags = array_merge($this->simpletags,$args);
}else{
$this->simpletags[] = $args;
}
}
function addregex($args){
if( is_array($args) ){
$this->regextags = array_merge($this->regextags,$args);
}else{
$this->regextags[] = $args;
}
}
function tagreplacer($tags = array(), $valuetags = array(),$replacetags = array(), $regextags = array()){
$this->simpletags = $tags;
$this->changingtags = $replacetags;
$this->regextags = $regextags;
$this->valuetags = $valuetags;
}
function dotags($text){
return preg_replace($this->search, $this->replace, $text);
}
function doreplacement(){
$this->search[0] = "~\[(".join("|", $this->simpletags)."|/".join("|/", $this->simpletags).")\s*?\]~is";
$this->replace[0] = '<$1>';
foreach( $this->changingtags as $s=>$r){
$this->search[] = '~\['.$s.'\](.*?)\[/'.$s.'\]~is';
$this->replace[] = $r[0].'$1'.$r[1];
}
foreach( $this->regextags as $s=>$r){
$this->search[] = $s;
$this->replace[] = $r;
}
$splitter = "__THIS_SPLITS_IT__";
$splitmes = array();
foreach( $this->callbacks as $tag=> $handler){
$splitmes[] = '~\[('.$tag.')\](.*?)\[/'.$tag.'\]~is';
}
$this->text = preg_replace($splitmes, $splitter.'$1_$2'.$splitter, $this->text);
$parts = preg_split('~'.$splitter.'~is', $this->text);
$this->text = "";
// echo "<pre>";
// print_r($parts);
// echo "</pre>";
for( $i = 0; $i < count($parts); $i++){
if( $i % 2 == 0 ){
$this->text .= $this->dotags(nl2br(htmlentities($parts[$i])));
}else{
$tagpos = strpos($parts[$i], '_');
$tag = substr($parts[$i], 0, $tagpos);
$text = substr($parts[$i], $tagpos+1);
if( is_array($this->callbacks[$tag]) ){
$func = $this->callbacks[$tag][1];
$obj =&$this->callbacks[$tag][0];
$this->text .= $obj->$func($text);
}else{
$func = $this->callbacks[$tag];
$this->text .= $func($text);
}
}
}
}
function replace($text, $nl2br = false){
$this->text = &$text;
$this->doreplacement();
return $this->text;
}
function setcallbacktag($tag, $handler){
$this->callbacks[$tag] = $handler;
}
}
class bbtags extends tagreplacer{
function dophp(&$text, $tagvalue=""){
ob_start();
highlight_string($text);
$text = ob_get_contents();
ob_end_clean();
if( $tagvalue == "" ){
return '<div class="php">'.$text.'</div>';
}else{
return '<b>'.$tagvalue.'</b><div class="php">'.$text.'</div>';
}
}
function doeval(&$text){
ob_start();
eval($text);
$text = ob_get_contents();
ob_end_clean();
return '<b>Output:</b><div class="eval">'.$text.'</div>';
}
function dohtml(&$text){
return '<div class="html">'.$text.'</div>';
}
function docode(&$text){
$text = '<pre>'.htmlentities($text).'</pre>';
return $text;
}
function bbtags($admin = false){
$this->callbacks = array();
$this->simpletags = array("b", "i", "u", "p","ul", "li", "ol", "br");
$this->changingtags = array("heading" => array('<span style="font-size: 10pt; font-weight: bold; text-decoration:underline">','</span>'));
$this->regextags = array('~\[font\s*?=\s*?(.*?)\](.*?)\[/font\s*?\]~is' => '<font family="$1">$2</font>',
'~\[size\s*?=\s*?(.*?)\](.*?)\[/size\s*?\]~is' => '<font size="$1">$2</font>',
'~\[color\s*?=\s*?(.*?)\](.*?)\[/color\s*?]~is' => '<font color="$1">$2</font>',
'~\[url=(.*?)\](.*?)\[/url\]~is' => '<a target="_blank" href="$1">$2</a>',
'~\[url\](.*?)\[/url]~is' => '<a target="_blank" href="$1">$1</a>',
'~\[quote=(.*?)\](.*?)\[/quote\]~is' => '<br><b>Quote from $1:</b><div class="quote">$2</div>',
'~\[label=(.*?)\](.*?)\[/label\]~is' => '<a name="$1"></a>$2',
'~\[quote\](.*?)\[/quote]~is' => '<div class="quote">$1</div>',
'~\[email=(.*?)\](.*?)\[/email\]~is' => '<a href="mailto:$1">$2</a>',
'~\[email\](.*?)\[/email]~is' => '<a href="mailto:$1">$1</a>');
if( $admin ){
$this->addregex(array('~\[link=(.*?)\](.*?)\[/link\]~is' => '<a href="$1">$2</a>',
'~\[link\](.*?)\[/link]~is' => '<a href="$1">$1</a>',
'~\[img=(.*?)\](.*?)\[/img\]~is' => '<img src="$2" border="0" alt="$1" />',
'~\[img\](.*?)\[/img]~is' => '<img border="0" alt=""> src="$1" alt="$1">',));
$this->setcallbacktag("html", array($this, "dohtml"));
$this->setcallbacktag("eval", array($this, "doeval"));
}
$this->setcallbacktag("code", array($this, "docode"));
$this->setcallbacktag("php", array($this, "dophp"));
}
}
?>