MySQL highlighter

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

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

Post by ziggy3000 »

here's my entire code

Code: Select all

function mysql($sql){
	// highlighting the code

	$sql = str_replace("\n", '<br />', $sql);

	//Quotes
	$sql = preg_replace("/(['\"`])(.+?)(['\"`])/", "<span class='quote'>\\1\\2\\3</span>", $sql);
	$sql = preg_replace('/`(.+?)`/i', "<span class='quote'>`\\1`</span>", $sql);

	//Equal Sign
 	$sql = preg_replace("/(=)(?!['\"][^>]*>)/", "<span class='operator'>\\1</span>", $sql);

	//Keywords 	
	$sql = preg_replace("/((CREATE|ALTER|DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+(\w)*[^<>])/i","<span class='keyword'>\\1</span>", $sql);
	$sql = preg_replace("/(SELECT|UPDATE|INSERT)/i", "<span class='keyword'>\\1</span>", $sql);
	$sql = preg_replace("/(MAX|AVG|SUM|COUNT|MIN|FROM|INTO)/i", "<span class='keyword'>\\1</span>", $sql);
	 //edited out very long keyword highlighting


	//Condition
	$sql = preg_replace('/(LIKE|NOT LIKE|REGEXP)/i', "<span class='condition'>\\1</span>", $sql);

	//Operator
	$sql = str_replace(";", "<span class='operator'>;</span>", $sql);
	$sql = preg_replace("/[0-9]{50}/", "<span class='number'>\\1</span>", $sql);		
	
	//Numbers
	$sql = preg_replace("/(\d{1,10})/", "<span class='number'>\\1</span>", $sql);		

	// Comments
	$sql = preg_replace("'(\/\*.*)(\w*)(.*\*/)'", "<span class='comment'>\\1\\2\\3</span>", $sql);
	$sql = preg_replace("'((//|#)((\w|\s|\d)*)*)'i", "<span class='comment'>\\1</span>", $sql);

//	$sql = preg_replace("/()/i", "<span class=''>\\1</span>", $sql);		
	
	// output the Code
	
	echo $this->output($sql, "grey");	
}
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

yea, that is not working for me. thats removing all text on that line except for // and # and even removes the break tag.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

i found out why. it was a pretty stupid mistake. now it highlights everything but the numbers.
here's my html output

Code: Select all

<span class='comment'>// Test Comment number <span class='number'>1</span>
<br /></span><span class='comment'># This is the <span class='number'>2</span>nd comment
<br /></span>
i wonder why the css you gave me before isn't overwriting the color of the numbers?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

You have to wrap up the entire expression as a subpattern, you know...

Edit: Show me the CSS you are using.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

thats how i got it to work i mentioned that in the previous post
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

Code: Select all

<style>
	.operator{
		color: green;
	}
	.keyword{
		color: blue;
	}
	.comment{
		color: orange;
	}
	.comment *{
		color: orange;
	}
	.quote{
		color: red;
	}
	.number{
		color: darkblue;
	}
	.condition{
		color: orange;
	}
	code { padding-top: 2px; padding-right: 2px; padding-bottom: 2px; padding-left: 2px }
</style>
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

i have also encountered this problem for /* and */ type comments
it's just not overwriting the colors for the numbers. it overhighlights all other html tags
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

CSS is all about inheritance and overriding. You have the comment * too high up. Put it below the other styles, as they are overwriting it.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

thanks ^_^
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Look at that. Only took us a couple hundred posts. :P
Post Reply