Page 1 of 13

MySQL highlighter

Posted: Fri May 25, 2007 5:12 pm
by ziggy3000
i am creating a mysql highlighter
and i need help.

what should i put in the replacement part between the 2 div tags?

Code: Select all

<?php
$sql = preg_replace('/^(CREATE|ALTER|DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', "<div style='color:blue'></div>", $sql);
?>

Posted: Fri May 25, 2007 5:53 pm
by John Cartwright
Have you looked at http://www.php.net/function.preg_replace to see how to reference your matches?

Posted: Fri May 25, 2007 6:06 pm
by ziggy3000
would it be

'\\1\\2 '

if it isn't, can u just tell me what it is?

Posted: Fri May 25, 2007 6:09 pm
by John Cartwright
try it, it is all clearly documented under the replacement parameter header

Posted: Fri May 25, 2007 6:10 pm
by ziggy3000
it doesnn't work :(

Posted: Fri May 25, 2007 6:20 pm
by ziggy3000
i'm trying to parse CREATE DATABASE, but it doesn't highlight...

Posted: Fri May 25, 2007 6:38 pm
by John Cartwright
CREATE DATABASE
your reguar expression requires one or more spaces after DATABASE

Posted: Fri May 25, 2007 6:40 pm
by ziggy3000
in the code i am parsing it is, i just didn't show it up here

the actual code i am parsing is
CREATE DATABASE hi

Posted: Fri May 25, 2007 6:41 pm
by John Cartwright

Code: Select all

$sql = preg_match(..., "<div style='color:blue'>$1 $2</div>", ...);
works for me

Posted: Fri May 25, 2007 6:44 pm
by ziggy3000
it's echoing 0.
can you display the full code you're using, and what you're entering

Posted: Fri May 25, 2007 6:45 pm
by ziggy3000
nevermind, it works

Posted: Fri May 25, 2007 6:48 pm
by John Cartwright
your welcome :wink:

Posted: Fri May 25, 2007 6:51 pm
by RobertGonzalez
I thought GeSHi parsed SQL as a language, in addition to PHP. That might be a good place to learn how to make highlighters and parsers.

Posted: Fri May 25, 2007 6:53 pm
by John Cartwright
Everah wrote:I thought GeSHi parsed SQL as a language, in addition to PHP.

Code: Select all

SELECT `answer` FROM `jcart` LIMIT 1
You thought right

Posted: Fri May 25, 2007 6:57 pm
by RobertGonzalez
I thought so. Of course, I am also the guy that thought it would be easier to download a complete application instead of rebuilding it from scratch without a thorough understanding of regular expressions, too. So what do I know? ;)