zie ook [url]http://casteleyn.no-ip.info:8080/~Hipska/Extra/classes/UBB/index.php[/url]

class.ubb.php
[code]<?php

/***********************************************
	
	Name: UBB parser
	Version: 1.1
	Author: Hipska, thanks to Jelmer and Willem-Jan
	License: GNU/GPL
	
***********************************************/

class ubb {
	
	var $code	= array();
	var $smiley	= array(
		"&lt;:-P"	=> 'Angry-Tongue.png',
		"&lt;:P"	=> 'Angry-Tongue.png',
		"&lt;:-("	=> 'Angry.png',
		"&lt;:("	=> 'Angry.png',
		":-]"	=> 'Blushing.png',
		":]"	=> 'Blushing.png',
		":-?"	=> 'Confused.png',
		":?"	=> 'Confused.png',
		"B-)"	=> 'Cool.png',
		"B)"	=> 'Cool.png',
		"8-)"	=> 'Cool.png',
		"8)"	=> 'Cool.png',
		":'("	=> 'Crying.png',
		":'-("	=> 'Crying.png',
		":-["	=> 'Embarrassed.png',
		":["	=> 'Embarrassed.png',
		"&lt;:-D"	=> 'Evil.png',
		"&lt;:D"	=> 'Evil.png',
		":-("	=> 'Frown.png',
		":("	=> 'Frown.png',
		":-O"	=> 'Gasp.png',
		":O"	=> 'Gasp.png',
		"X-("	=> 'Grimmace.png',
		"X("	=> 'Grimmace.png',
		":-D"	=> 'Grin.png',
		":D"	=> 'Grin.png',
		";-D"	=> 'Grinning-Wink.png',
		";D"	=> 'Grinning-Wink.png',
		"O:-)"	=> 'Innocent.png',
		"0:)"	=> 'Innocent.png',
		":-*"	=> 'Kiss.png',
		":*"	=> 'Kiss.png',
		":-X"	=> 'Lips-Are-Sealed.png',
		":X"	=> 'Lips-Are-Sealed.png',
		"&lt;:-)"	=> 'Mischievous.png',
		"&lt;:)"	=> 'Mischievous.png',
		':-$'	=> 'Money-Mouth.png',
		':$'	=> 'Money-Mouth.png',
		"&lt;:-|"	=> 'Not-Amused.png',
		"&lt;:|"	=> 'Not-Amused.png',
		":-!"	=> 'Oops.png',
		":!"	=> 'Oops.png',
		":-@"	=> 'Shouting.png',
		":@"	=> 'Shouting.png',
		"|-)"	=> 'Sleeping.png',
		"I-)"	=> 'Sleeping.png',
		":-)"	=> 'Smile.png',
		":)"	=> 'Smile.png',
		":-&lt;"	=> 'Smirk.png',
		":&lt;"	=> 'Smirk.png',
		":-|"	=> 'Straight-Faced.png',
		":|"	=> 'Straight-Faced.png',
		":-I"	=> 'Straight-Faced.png',
		":I"	=> 'Straight-Faced.png',
		":-P"	=> 'Tongue.png',
		":P"	=> 'Tongue.png',
		":-\\"	=> 'Undecided.png',
		":\\"	=> 'Undecided.png',
		";-P"	=> 'Wink-Tongue.png',
		";P"	=> 'Wink-Tongue.png',
		";-)"	=> 'Wink.png',
		";)"	=> 'Wink.png'
	); // standard smileys
	var $text	= '';
	var $size	= 300; // standard thumbnail size (px)
	
	function ubb($size = null, $smileys = null){
		if(is_array($smileys))	$this->smiley = $smileys;
		if(is_numeric($size))	$this->size = $size;
	}
	
	function ubb_ignore($string = '', $return = false){
		
		if(!empty($string)) $this->setText($string);
		$this->text = preg_replace('#\[ignore\](.*?)\[/ignore\]#sie',"\$this->_ignore('\\1')",$this->text);
		if($return) return $this->getText();
	}
	function ubb_code($string = '', $return = false){
		
		if(!empty($string)) $this->setText($string);
		
		$this->text = preg_replace('#\[code\](.*?)\[/code\]#sie',"\$this->_highlight('\\1')",$this->text);
		$this->text = preg_replace('#\[code=(.+?)\](.*?)\[/code\]#sie',"\$this->_highlight('\\2','\\1')",$this->text);
		$this->text = preg_replace('#<\?(.*?)\?>#sie',"\$this->_highlight('<?\\1?>')",$this->text);
		
		if($return) return $this->getText();
	}
	function ubb_html($string = '', $return = false){
		
		if(!empty($string)) $this->setText($string);
		
		// HTML codes wegwerken
 		$this->text = htmlspecialchars($this->text);
 		// newlines maken
 		$this->text = nl2br($this->text);
 		
 		if($return) return $this->getText();
	}
	function ubb_opmaak($string = '', $return = false){
		
		if(!empty($string)) $this->setText($string);
		
		// Cursief
		$this->text = preg_replace('#\[i\](.+?)\[/i\]#si','<span style="font-style: italic;">\\1</span>',$this->text);
		// Onderstreept
		$this->text = preg_replace('#\[u\](.+?)\[/u\]#si','<span style="text-decoration: underline;">\\1</span>',$this->text);
		// Vetgedrukt
		$this->text = preg_replace('#\[b\](.+?)\[/b\]#si','<span style="font-weight: bold;">\\1</span>',$this->text);
		// Doorstreept
		$this->text = preg_replace('#\[s\](.+?)\[/s\]#si','<span style="text-decoration: line-through;">\\1</span>',$this->text);
		// Kleur
		$this->text = preg_replace('#\[color=(.+?)\](.+?)\[/color\]#si','<span style="color: \\1;">\\2</span>',$this->text);
		// Grootte
		$this->text = preg_replace('#\[size=([1-9]{1})\](.+?)\[/size\]#si','<span style="font-size: \\1ex;">\\2</span>',$this->text);
		$this->text = preg_replace('#\[size=([a-z\-]{5,8})\](.+?)\[/size\]#si','<span style="font-size: \\1;">\\2</span>',$this->text);
		// Uitlijning
		$this->text = preg_replace('#\[align=([a-z]{4,7})\](.+?)\[/align\]#si','<div style="text-align: \\1;">\\2</div>',$this->text);

		if($return) return $this->getText();
	}
	function ubb_url($string = '', $return = false){
		
		if(!empty($string)) $this->setText($string);
		
		// Links maken met behulp van tags
		$this->text = preg_replace('#\[url\]((([a-z0-9]{1,6}:[/]{2}|\#|\?)|([a-z0-9]{1,6}:))(.+?))\[/url\]#si','<a href="\\1">\\3\\5</a>',$this->text);
		$this->text = preg_replace('#\[url\](.+?)\[/url\]#si','<a href="http://\\1">\\1</a>',$this->text);
		// Links maken met titel
		$this->text = preg_replace('#\[url=([a-z0-9]{1,6}:(.+?))\](.+?)\[/url\]#si','<a href="\\1">\\3</a>',$this->text);
		$this->text = preg_replace('#\[url=(.+?)\](.+?)\[/url\]#si','<a href="http://\\1">\\2</a>',$this->text);
		// Mailto links
		$this->text = preg_replace('#\[mail\](.+?)\[/mail\]#si','<a href="mailto:\\1">\\1</a>',$this->text);
		// Skype links
		$this->text = preg_replace('#\[skype\](.+?)\[/skype\]#si','<a href="callto:\\1">\\1</a>',$this->text);
		if($return) return $this->getText();
	}
	function ubb_autourl($string = '', $return = false){
		
		if(!empty($string)) $this->setText($string);
		
		// Automatisch links maken
		$this->text = preg_replace('#(^|[ \n\r\t])([a-z0-9]{1,6}://([a-z0-9\-]{1,}(\.?)){1,}[a-z]{2,5}(:[0-9]{2,5}){0,1}((\/|~|\#|\?|=|&amp;|&|\+){1}[a-z0-9\-._%]{0,}){0,})#si', '\\1<a href="\\2">\\2</a>',$this->text);
		$this->text = preg_replace('#(^|[ \n\r\t])((www\.){1}([a-z0-9\-]{1,}(\.?)){1,}[a-z]{2,5}(:[0-9]{2,5}){0,1}((\/|~|\#|\?|=|&amp;|&|\+){1}[a-z0-9\-._%]{0,}){0,})#si', '\\1<a href="http://\\2">\\2</a>',$this->text);
		$this->text = preg_replace('#(^|[ \n\r\t])(([a-z0-9\-_]{1,}(\.?)){1,}@([a-z0-9\-]{1,}(\.?)){1,}[a-z]{2,5})#si', '\\1<a href="mailto:\\2">\\2</a>',$this->text);
		
		if($return) return $this->getText();
	}
	function ubb_img($string = '', $return = false){
		
		if(!empty($string)) $this->setText($string);
		
		// Plaatjes
		$this->text = preg_replace('#\[img\](.+?)\[/img\]#si','<a href="\\1" rel="lightbox"><img src="'.INCLUDES.'image.php?file=[img]\\1[/img]&amp;size='.$this->size.'" alt="Afbeelding"/></a>',$this->text);
		$this->text = preg_replace('#\[img=(.+?)\](.+?)\[/img\]#si','<a href="\\2" rel="lightbox" title="\\1"><img src="'.INCLUDES.'image.php?file=[img]\\2[/img]&amp;size='.$this->size.'" alt="\\1"/></a>',$this->text);
		$this->text = preg_replace('#\[img\](.+?)\[/img\]#sie','urlencode("\\1")',$this->text);
		
		if($return) return $this->getText();
	}
	function ubb_smiley($string = '', $return = false){
		
		if(!empty($string)) $this->setText($string);
		
		// Smileys
		foreach ($this->smiley as $key => $img) {
			$this->text = str_replace(' '.$key,' <img src="'.INCLUDES.'images/smilies/'.$img.'" alt="'.$key.'"/>',$this->text);
			$this->text = str_replace($key.' ','<img src="'.INCLUDES.'images/smilies/'.$img.'" alt="'.$key.'"/> ',$this->text);
		//	$this->text = preg_replace('#(^|[ \n\r\t]?)('.quotemeta($key).')(^|[ \n\r\t]?)#is','\\1<img src="'.INCLUDES.'images/smilies/'.$img.'" alt="'.$key.'" />\\3',$this->text);	// commented because it gives a max execution time limit error ...
		}
		
		if($return) return $this->getText();
	}
	function ubb_quote($string = '', $return = false){
		if(!empty($string)) $this->setText($string);
		
		// qoute
		$this->text = preg_replace('#\[quote\](<br />)(.+?)\[/quote\]#si','<div class="Block"><small>Quote</small><div class="Quote">\\2</div></div>',$this->text);
 		$this->text = preg_replace('#\[quote=(.+?)\](<br />)(.+?)\[/quote\]#si','<div class="Block"><small>Quote: <b>\\1</b></small><div class="Quote">\\3</div></div>',$this->text);
		
		if($return) return $this->getText();
	}
	function ubb_list($string = '', $return = false){
		if(!empty($string)) $this->setText($string);
		
		// make an ordered or un-ordered list
		$this->text = preg_replace('#\[ul\](<br />){0,}(.+?)\[/ul\](<br />){0,}#si','<ul>\\2</ul>',$this->text);
		$this->text = preg_replace('#\[ol\](<br />){0,}(.+?)\[/ol\]#si','<ol>\\2</ol>',$this->text);
		
		$this->text = preg_replace('#\[li\](.+?)\[/li\](<br />){0,}#si','<li>\\1</li>',$this->text);
		
		if($return) return $this->getText();
	}
	function ubb_search($string = '', $return = false){
		if(!empty($string)) $this->setText($string);
		
		// search words (word1+word2)
		$search = array();
 		$this->text = preg_replace('#\[search=((([a-z_0-9]{1,})(\+?)){1,})\](.+?)\[/search\]#sie','$this->_search("\\1","\\5")',$this->text);
		
		if($return) return $this->getText();
	}
	function ubb_spoil($string = '', $return = false){
		if(!empty($string)) $this->setText($string);
		
		// spoiler
 		$this->text = preg_replace('#\[spoiler=(.+?)\](.+?)\[/spoiler\]#sie',"'<input type=\"button\" value=\"\\1\" onclick=\"javascript:ShowHide(\'spoil_'.md5('\\1').'\');\" /><div class=\"Block\"><div id=\"spoil_'.md5('\\1').'\" class=\"Spoiler\">'.stripslashes('\\2').'</div></div>'",$this->text);
		
		if($return) return $this->getText();
	}
	
	function parse($string = '', $tags = array()){
		if(!empty($string)) $this->setText($string);
		foreach ($tags as $tag){
			$method = 'ubb_'.$tag;
			if(method_exists($this,$method)) $this->$method();
		}
		return $this->getText();
	}
	function parse_all($string = ''){
		if(!empty($string)) $this->setText($string);
		
		$methods = get_class_methods($this);
		foreach ($methods as $method){
			$sub = explode('_',$method);
			if($sub[0] == 'ubb' and count($sub) > 1) $this->$method();
		}
		return $this->getText();
	}
	
	function setText($string){
		$this->text = $string;
		$this->code = array();
	}
	function getText(){
		$this->text = preg_replace('#\[highlight\]([0-9])\[/highlight\]#sie','$this->_highlight(\\1)',$this->text);
		$this->text = preg_replace('#\[highlight=(.+?)\]([0-9])\[/highlight\]#sie','$this->_highlight(\\2,"\\1")',$this->text);
		$this->text = preg_replace('#\[ignore\]([0-9])\[/ignore\]#sie','$this->_ignore(\\1)',$this->text);
		return $this->text;
	}
	
	function _highlight($code, $file = false){
		if(is_numeric($code)){
			$highlight = '<div class="Block"><small>Code';
			if($file) $highlight .= ': <b>'.$file.'</b>';
			$highlight .= '</small><div class="Code"><div class="CodeLines">';		
			$n = 0; $lines = explode("\n",$this->code[$code]);
			
			$code = highlight_string($this->code[$code], true);
   			$code = str_replace(array('<font ', '</font>', '<code>', '</code>'), array('<span ', '</span>', '', ''), $code);
   			$code = preg_replace('#color="(.*?)"#', 'style="color: \\1;"', $code);
   			
			while ($n < count($lines)) $highlight .= ++$n.'<br />';
			$highlight .= '</div><div class="CodeCode">'.
			substr($code,30,-8)
			.'</div></div></div>';
			return $highlight;
			
		}else{
			$n = array_push($this->code,trim(str_replace("\\\"", "\"", $code)));
			if($file) return '[highlight='.$file.']'.--$n.'[/highlight]';
			else return '[highlight]'.--$n.'[/highlight]';
		}
	}
	function _ignore($code){
		if(is_numeric($code)){
			return $this->code[$code];
		}else{
			$n = array_push($this->code,nl2br(htmlspecialchars($code)));
			return '[ignore]'.--$n.'[/ignore]';
		}
	}
	function _search($search,$text){
		$search = explode('+',$search);
		foreach ($search as $word){
			$color = '#';
			for ($i = 0; $i < 3; $i++ )
				$color .= str_pad(dechex(( round(rand(0,255)/51) * 51 )), 2, '0', STR_PAD_LEFT);
			$text = preg_replace('#(^|[ \n\r\t])(' . trim($word) . ')(^|[ \n\r\t\,\.\;\:])#si', '\\1<span style="background-color: '.$color.';">\\2</span>\\3', $text);
		}
		return $text;
	}
	
}
?>[/code]

image.php

[code]<?php

$default_img = 'images/no-image.jpg';
//var_dump(gd_info()); // look if you have libGD installed <-- zeker eens doen!

// set the default image
$img = getimagesize($default_img);
list($width, $height, $type) = $img;
$default_img = array(	'width' => $width, 'height' => $height,
						'new_width' => $width, 'new_height' => $height,
						'mime' => $img['mime'], 'type' => $type,
						'file' => $default_img );

// set the max size
if(isset($_GET['size'])) $max = (int) $_GET['size'];
else $max = $width;

// prepare image sizes
if(isset($_GET['file']) and $img = @getimagesize($_GET['file'])){
	
	list($width, $height, $type) = $img;
	
	if($width > $max){
		$nwidth = $max;
		$nheight = (int) round($height/($width/$max));
	}else {
		$nwidth = $width;
		$nheight = $height;
	}
	
	$img = array(	'width' => $width, 'height' => $height,
					'new_width' => $nwidth, 'new_height' => $nheight,
					'mime' => $img['mime'], 'type' => $type,
					'file' => $_GET['file'] );
	
}else $img = $default_img;
//var_dump($img);

// load image
/* easier script found
switch ($img['type']){
	case IMG_JPEG: $data = imagecreatefromjpeg($img['file']); break;
	case IMG_GIF: $data = imagecreatefromgif($img['file']); break;
	case IMG_PNG: $data = imagecreatefrompng($img['file']); break;
} */
$data = imagecreatefromstring(file_get_contents($img['file']));

// resize image
$tmp_data = imagecreatetruecolor($img['new_width'], $img['new_height']);
imagecopyresampled($tmp_data, $data, 0,0,0,0, $img['new_width'], $img['new_height'], $img['width'], $img['height']);
imagedestroy($data);
$data = $tmp_data;

// return image
header('Content-type: image/png');
imagepng($data);
imagedestroy($data);

?>[/code]

css/ubb.css

[code]/* CSS voor UBB Code */
img {
	border-style: none;
	vertical-align: middle;
	margin: 2px;
}
.Block {
	margin: 10px 5%;
}
.Quote {
	border: 1px dashed; padding: 10px;
}
.Code {
	border: 1px dotted;
	background-color: #fcff9f;
	font: small "Courier New", Courier, mono;
	white-space: nowrap;
}
.CodeLines {
	text-align: right;
	float: left;
	padding: 4px 2px;
}
.CodeCode {
	overflow: auto;
	margin-right: 1px;
	padding: 4px;
	background-color: #fff;
}
.Spoiler {
	display: none;
	border: 1px dotted;
	padding: 10px;
}[/code]

voorbeeld.php

[code]<?php
define('INCLUDES', './');
include('class.ubb.php');

$tags = array('ignore','html','opmaak','url','img','quote'); // deze tags zal hij uitvoeren ubb_TAG(); (kijk in de klasse wat het precies doet)
$text = 'Tekst met ubb code';

$ubb = new ubb();

echo $ubb->parse_all($text); // deze functie doet alle ubb code's
// of : 
echo $ubb->parse($text, $tags); // deze functie doet enkel de code's die jij wil

?>[/code]