Page 1 of 1

OOP bbcode parser

Posted: Sun Aug 12, 2007 9:50 pm
by ziggy3000
now that i have gotten a little more advanced in php, i have coded this OOP bbcode parser. the main problem is that i want the way that the user inputs the bbcode, is the way it should output. but when you use the php tag in my parser, it always put's it above all other bbcode :(. is it the way that highlight_string() works or what?
here is my full code

Code: Select all

<?php
//
// BBcode.php
// Desc: Parses BBcode
//

class bbcode{
	
	function parse($code){
		$code = preg_replace( "/<br>|<br \/>/", "\n", $code);
		
		////////////////////
		//	Parse the BBcode
		////////////////////
		
		// First the

Code: Select all

tags
		$code = preg_replace_callback( "#\[code\](.+?)\[/code\]#is", array( &$this, 'code' ), $code);
		
		// Automatically parse urls
		$code = preg_replace_callback("#(^|\s|>)((http|https|news|ftp)://\w+[^\s\[\]\<]+)#i", array( &$this, 'autourl' ), $code);
		
		// More Code
		$code = preg_replace_callback( "#\[php\](.+?)\[/php\]#is", array( &$this, 'php' ), $code);
		
		// Img and flash tags
		$code = preg_replace_callback("#\[img\](.+?)\[/img\]#i", array( &$this, 'img'), $code);
		
		// Basic stuff
		$code = $this->simple_tags("b", "b", $code);
		$code = $this->simple_tags("u", "u", $code);
		$code = $this->simple_tags("i", "i", $code);
		$code = $this->simple_tags("s", "strike", $code);
		$code = $this->simple_tags("sup", "sup", $code);
		$code = $this->simple_tags("sub", "sub", $code);
		
		// (c), (tm) and (r)
		$code = preg_replace( "#\(c\)#i"     , "&copy;" , $code );
		$code = preg_replace( "#\(tm\)#i"    , "™" , $code );
		$code = preg_replace( "#\(r\)#i"     , "&reg;"  , $code );
		
		// [email]abc@index.com[/email]
		// [email=abc@index.com]Email me[/email]
		$code = preg_replace( "#\[email\](\S+?)\[/email\]#i"                                                                , "<a href=\"mailto:\\1\">\\1</a>", $code );
		$code = preg_replace( "#\[email\s*=\s*\&quot\;([\.\w\-]+\@[\.\w\-]+\.[\.\w\-]+)\s*\&quot\;\s*\](.*?)\[\/email\]#i"  , "<a href=\"mailto:\\1\">\\2</a>", $code );
		$code = preg_replace( "#\[email\s*=\s*([\.\w\-]+\@[\.\w\-]+\.[\w\-]+)\s*\](.*?)\[\/email\]#i"                       , "<a href=\"mailto:\\1\">\\2</a>", $code );
		
		// [url]http://www.index.com[/url]
		// [url=http://www.index.com]forum[/url]
		$code = preg_replace_callback("#\[url\](.*?)\[/url\]#is", array( &$this, 'url'), $code);
		$code = preg_replace_callback("#\[url=(?:\&quot\;|\")(.*?)(?:\&quot\;|\")\](.*?)\[\/url\]#is", array( &$this, 'url'), $code);
		
		//replace the the line breaks back
		$code = str_replace("\n", "<br />", $code);
		
		return $code;		
	}
	
	function code($matches = array()){
		
		//-----------------------------------------
		// Reserve Indentations
		//-----------------------------------------
		
		$txt = rtrim( $matches[1] );
		$txt = preg_replace( "#^(\n+)(.+?)$#s", "\\2", $txt );
		
		if ( $txt == "" )
		{
			return;
		}
		
		//-----------------------------------------
		// remove most of the common smily characters.
		//-----------------------------------------
		
		$txt = str_replace( "<"         , "<" , $txt );
		$txt = str_replace( ">"         , ">" , $txt );
		$txt = str_replace( "<"      , "<" , $txt );
		$txt = str_replace( ">"      , ">" , $txt );
		$txt = str_replace( """    , """ , $txt );
		$txt = str_replace( ":"         , ":" , $txt );
		$txt = str_replace( "["         , "[" , $txt );
		$txt = str_replace( "]"         , "]" , $txt );
		$txt = str_replace( ")"         , ")" , $txt );
		$txt = str_replace( "("         , "(" , $txt );
		$txt = str_replace( "\r"        , "<br />", $txt );
		$txt = str_replace( "\n"        , "<br />", $txt );
		$txt = preg_replace( "#\s{1};#" , ";" , $txt );
		
		//-----------------------------------------
		// Ensure that spacing is preserved
		//-----------------------------------------
		
		$txt = preg_replace( "#\t#"   , "&nbsp;&nbsp;&nbsp;&nbsp;", $txt );
		$txt = preg_replace( "#\s{2}#", "&nbsp;&nbsp;"            , $txt );
		
		return "<code>$txt</code>";
		
	}
	
	function simple_tags($tag, $html_tag, $source){
		$default = "#\[".$tag."\](.*?)\[\/".$tag."\]#is";
		$replace = "<".$html_tag.">$1</".$html_tag.">";
		$txt = preg_replace($default, $replace, $source);
		return $txt;
	}
	
	function img($matches = array()){
		$url = trim($matches[1]);
		//-----------------------------------------
		// XSS check
		//-----------------------------------------
		
		$url = urldecode( $url );
		$url = str_replace( "document.cookie", "" , $url );
		
		return "<img src='".$url."' border='0' />";
	}
	
	function autourl($matches = array()){
		$url = trim($matches[2]);
		return "<a href='".$url."'>".$url."</a>";
	}
	
	function url($matches = array()){
		$url = $matches[1];
		$url = trim($url);
		$txt = $matches[2] ? $matches[2] : $matches[1];
		
		return "<a href='".$url."'>".$txt."</a>";
	}
	
	function php($matches = array()){
		$txt = rtrim( $matches[1] );
		$txt = preg_replace( "#^(\n+)(.+?)$#s", "\\2", $txt );
		
		if ( $txt == "" )
		{
			return;
		}
			
		$code = highlight_string($txt);
		return "<code>".$code."</code><br>";
	}
	
}

?>

Posted: Sun Aug 12, 2007 9:58 pm
by John Cartwright
You need to specify the second parameter in highlight_string() as true.

Posted: Sun Aug 12, 2007 10:59 pm
by RobertGonzalez
<off-topic>
Why are you passing the match array around as a parameter to each method? Why not just set an object property as use that? Just a thought.
</off-topic>

Posted: Sun Aug 12, 2007 11:09 pm
by s.dot
$txt = str_replace( """ , """ , $txt );

need "\""

Posted: Mon Aug 13, 2007 9:11 am
by ziggy3000
oh, that part is actually hex values.
& #34;
but without the spaces. the phpbb parsing probably parsed that.

and thanks jcart. :)