OOP bbcode parser

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

OOP bbcode parser

Post 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>";
	}
	
}

?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

You need to specify the second parameter in highlight_string() as true.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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>
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

$txt = str_replace( """ , """ , $txt );

need "\""
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

oh, that part is actually hex values.
& #34;
but without the spaces. the phpbb parsing probably parsed that.

and thanks jcart. :)
Post Reply