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 »

when i am trying that, it parses
'color:green'
> =
unhighlighted instead of
=
here is the code i am parsing

Code: Select all

$sql = preg_replace('/(=)/', "<div style='color:green'> \\1 </div>", $sql);
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Umm... "color: green" isn't easy to differentiate from black on a white background. The code you have works fine for me.

Maybe you want "background-color: green; color: white;" :wink:

And you'll probably want to use span elements instead of div elements.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

what code are you parsing

i tried parsing this
SELECT *FROM blah WHERE `me` = '$you'
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Code: Select all

echo preg_replace('/(=)/', '<span style="background-color: green; color: white;">\\1</span>', "hi = bye when me = gone");
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

now when i am trying to highlight all of the code, only the equal sign get's highlighted :(
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Yes... The equal sign is the only thing that you put into the regular expression.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

no, i meant that i have 10 more lines that replace and highlight the code, but now only the equal sign is getting highlighted, not the rest of the lines that i coded to replace & highlight

what i meant by that is i have the = replacer
and other code that replace words for it to be highlighted, but now, only the equal sign works. :(
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

last time I'm going to say this, post your code.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

k.

Code: Select all

<?php
function mysql($sql){

	// highlighting the code
	echo "<style>div
	{
	display: inline;
	}
	\n
	code { padding-top: 2px; padding-right: 2px; padding-bottom: 2px; padding-left: 2px }
	</style>\n\n";
	$sql = str_replace("\n", '<br />', $sql);

 	$sql = preg_replace('/(=)/i', "<span style='color: green'> \\1 </span>", $sql);

//	$sql = preg_replace("/(*)/", "<div style='color:brown'> \\1 </div>", $sql);
	$sql = preg_replace("/'(.+?)'/i", "<div style='color:red'> '\\1' </div>", $sql);
	$sql = preg_replace('/^(CREATE|ALTER|DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', "<div style='color:blue'>$1 $2 </div>", $sql);
	$sql = preg_replace('/.(SELECT|UPDATE)\s+/i', "<div style='color:blue'>\\1 </div>", $sql);
	$sql = preg_replace('/.(MAX|AVG|SUM|COUNT|MIN|FROM|INTO)\s+/i', "<div style='color:green'> \\1 </div>", $sql);
	$sql = preg_replace('/`(.+?)`/i', "<div style='color:red'> `\\1` </div>", $sql);
	$sql = preg_replace('/(ASC|DESC|ORDER BY|LIMIT|LEFT|JOIN|WHERE|MODIFY|CHANGE|DISTINCT)/i', "<div style='color:green'> \\1 </div>", $sql);
	$sql = preg_replace('/(LIKE|NOT LIKE|REGEXP)/i', "<div style='color:orange'> \\1 </div>", $sql);
	$sql = preg_replace('/"(.+?)"/i', "<div style='color:red'> \"\\1\" </div>", $sql);
	$sql = preg_replace('/(INT|VARCHAR|TINYINT|BIGINT|BINARY|BIT|BLOB|BOOL|BOOLEAN|CHAR|CHARACTER'.
'|DATE|DATETIME|DEC|DECIMAL|DOUBLE|ENUM|FLOAT|FLOAT4|FLOAT8|INT1|INT2|INT3|INT4|'.
'INT8|INTEGER|LONGBLOB|LONGTEXT|MEDIUMBLOB|MEDIUMTEXT|MEDIUMINT|MIDDLEINT|NCHAR|'.
'NUMERIC|REAL|SERIAL|SET|SMALLINT|TEXT|TIME|TIMESTAMP|TINYBLOB|TINYTEXT|'.
'VARBINARY|YEAR|PRIMARY|AUTO INCREMENT)/i', "<div style='color:darkred'> \\1 </div>", $sql);
	$sql = preg_replace('/;/i', "<b><div style='color: green;'> ;</div></b>", $sql);
	$sql = preg_replace('/[0-9]{6}/i', "<div style='color: red;'> \\1 </div>", $sql);		
//	$sql = preg_replace('/()/i', "<div style='color:'> \\1 </div>", $sql);		
	//$sql = stripslashes($sql);
	
}
?>
Last edited by ziggy3000 on Tue May 29, 2007 6:44 pm, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

When you take out all the bits after it does it work?

By the way:

Code: Select all

$sql = preg_replace('/(=)/i', "<span style='color: green'> \\1 </span>", $sql);
Would be a lot faster as:

Code: Select all

$sql = str_replace('=', "<span style='color: green'>=</span>", $sql);
Have you ever considered that what you're replacng is adding "=" itself so if this gets run more than once it will mess up your markup. You'll need to view the source code it's producing.

EDIT: I can see one line further down your code that's going to screw it up:

Code: Select all

$sql = preg_replace("/'(.+?)'/i", "<div style='color:red'> '\\1' </div>", $sql);
That replaces "anything" between ' and ' which will inevitably parse you style='' tags.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Hmmm, the more I look at your code the more I realise how wrongly you've gone about it. I'd still advise using GeSHi ;)

Because you're replacing the string inline and then looking for further syntax you're creating conflicts by parsing your own code. The correct approach is to tokenize the string with those tokens you're looking for, collected into an array or some other container, then loop through that rebuilding it with your styles applied.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

the way you told me to fix me code, now it echos
Warning: preg_replace() [function.preg-replace]: No ending delimiter '=' found in C:\highlighter.php on line 45
so that's why i didn't use that
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

now i changed it to this


Code: Select all

$sql = str_replace("\n", '<br />', $sql);
	$sql = preg_replace("/'(.+?)'/i", "<div style='color:red'> '\\1' </div>", $sql);
	$sql = preg_replace('/"(.+?)"/i', "<div style='color:red'> \"\\1\" </div>", $sql);
	$sql = preg_replace('/`(.+?)`/i', "<div style='color:red'> `\\1` </div>", $sql);
 	$sql = str_replace('/=/', "<span style='color: green'>=</span>", $sql);
//	$sql = preg_replace("/(*)/", "<div style='color:brown'> \\1 </div>", $sql);
	$sql = preg_replace('/^(CREATE|ALTER|DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', "<div style='color:blue'>$1 $2 </div>", $sql);
	$sql = preg_replace('/.(SELECT|UPDATE)\s+/i', "<div style='color:blue'>\\1 </div>", $sql);
	$sql = preg_replace('/.(MAX|AVG|SUM|COUNT|MIN|FROM|INTO)\s+/i', "<div style='color:green'> \\1 </div>", $sql);
	$sql = preg_replace('/(ASC|DESC|ORDER BY|LIMIT|LEFT|JOIN|WHERE|MODIFY|CHANGE|DISTINCT)/i', "<div style='color:green'> \\1 </div>", $sql);
	$sql = preg_replace('/(LIKE|NOT LIKE|REGEXP)/i', "<div style='color:orange'> \\1 </div>", $sql);
	$sql = preg_replace('/(INT|VARCHAR|TINYINT|BIGINT|BINARY|BIT|BLOB|BOOL|BOOLEAN|CHAR|CHARACTER'.
'|DATE|DATETIME|DEC|DECIMAL|DOUBLE|ENUM|FLOAT|FLOAT4|FLOAT8|INT1|INT2|INT3|INT4|INT8|INTEGER|'.
'LONGBLOB|LONGTEXT|MEDIUMBLOB|MEDIUMTEXT|MEDIUMINT|MIDDLEINT|NCHAR|NUMERIC|REAL|SERIAL|SET|'.
'SMALLINT|TEXT|TIME|TIMESTAMP|TINYBLOB|TINYTEXT|VARBINARY|YEAR|PRIMARY|AUTO INCREMENT)/i', "<div style='color:darkred'> \\1 </div>", $sql);
	$sql = preg_replace('/;/i', "<b><div style='color: green;'> ;</div></b>", $sql);
	$sql = preg_replace('/[0-9]{6}/i', "<div style='color: red;'> \\1 </div>", $sql);
EDIT: the previous post, i used preg instead of str. sorry

feyd | mmm long strings :roll:
Last edited by ziggy3000 on Tue May 29, 2007 3:12 pm, edited 1 time in total.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

i have noticed that people help me more when i dont post my whole code, because users just wait and see how i solve my problem, and then copy without learning...(most of them) :roll:
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

ziggy3000 wrote:i have noticed that people help me more when i dont post my whole code, because users just wait and see how i solve my problem, and then copy without learning...(most of them) :roll:
Kind of cocky to assume that if people aren't replying to you that they must be copying your code. I think it's safe to say that, being a forum of developers, we tend to write our code ourselves. And, so you know, most of us don't dwell on one post for too long, so when you have a long snippet of code (I'll admit), we don't always read the whole thing. It's not plain English, you know. :-p

On a lighter note, I assume you've at least been trying rather than coming back and forth here, hoping for a solution, right?
I don't have a solution to offer you, but I do have a suggestion. Use inline elements.


Is it still not working? What part of it isn't working?
Post Reply