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 »

yes i have been trying and also looking here just in case, but in one of me previous post, nobody really helped me. they just looked at it, then nothing. i had to find out the solution myself. and there could also be some guest that were just surfing the forum, and liked the code, and decided to copy it.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

:lol: I'm sure that any guest who read this thread would be more likely to get GeSHi than to steal your code. :-p
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I would tend to agree with you SD.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

so...
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

ziggy3000 wrote:so...
So... Is there still a problem in your code? Or should this thread be considered [Solved]?
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

yes! when it highlights = that's the only thing that gets highlighted! i want it to highlight all the stuff with out any html being highlighted
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Whoa whoa whoa. You mean that, in all of this code:

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);
The only thing that works is:

Code: Select all

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

Post by superdezign »

I tested your code. The equals sign is the only thing that doesn't work.

You have two errors in your regex though.

This:

Code: Select all

$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);
Should be this:

Code: Select all

$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);
(Remove the periods at the start of the regex)

And please, use inline elements. Span works fine. And on the spot <b> and <div style="">, you could condense it to simply <b style="">.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

k, i did what you said. here's my code now

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 style='color: green;'> ;</b>", $sql);
	$sql = preg_replace('/[0-9]{6}/i', "<div style='color: red;'> \\1 </div>", $sql);
so how should i get the equal sign to work?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Take the delimiters out of str_replace. str_replace does not run on regex.

And you're still not using inline elements! Span, man, span! :D
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

now it semi-echoing html code also...
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Completely clueless as to what you mean.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

now it echoing
='color:red'>
instead of the equal sign and the preg_replace above that code doesn't work
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I see you ignored d11's advise.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

i'm getting confused by reading his advise. i don't know what tokenize means and...
can someone translate that into basic english?
Post Reply