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

Post Reply
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It's a mode switch grouping. They aren't remembered like standard groups are. Basically, it's advisable to use them wherever remembering a grouping isn't important.

Forward slashes aren't escapes.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

?: is called a Non capturing Subpattern.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Okay, so that I'm clear, (?:pattern) means that pattern must exist, but that it shouldn't be collected, right? I guess it's just for times when a character class simply won't do....?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Character classes allow an arbitrary order. Subpatterns are specific patterns which may include character classes.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

I see. :-D

I always assumed that there was at least something that regex couldn't do. They thought of everything (or, made it up as they went along).
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

i only semi used your code. i added the css, and some patterns, but not all of your code(makes me feel good). but i will still try to give you credit :D

@d11wtq

i tried parsing
'hello `me` '
and it worked. 8)
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

what should i put in my pattern so i can make it not highlight html code because i want it to highlight the database name when someone tries to parse this
CREATE DATABASE COMPUTER
my current code would only highlight CREATE DATABASE, but i want it to highlight COMPUTER as well, but not highlight html code. and it shouldn't depend on how long the string is. the highlighter works great though.

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

Post by superdezign »

You're welcome.

And, personally, I wouldn't encourage users of the parser to use table names without quoting them, but if you want to, you can.

If you open MySQL, you can run "help" on any operation. What you are shown is the basic usage layout. Make use of it, and you should be able to do anything with those queries using regex.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

i'm not really good at regex. how should i exclude html from the regex pattern?
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

nevermind, i found a really good site that teaches about regex, but i have one questions. what would i have as my regex if i want /* */ type comments?
the really good site
so far this is my pattern

Code: Select all

/((/\\*)(.+?)(\\*/))/
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

I don't think you need two slashes to escape the asterisk character. However, you do need to escape the '/' character because it is the delimiter. Also, I wouldn't put in so many subpatterns.

And just FYI, that regex doesn't allow for whitespace or newlines in comments, which is often why people use that style of commenting.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

superdezign wrote:I don't think you need two slashes to escape the asterisk character. However, you do need to escape the '/' character because it is the delimiter. Also, I wouldn't put in so many subpatterns.

And just FYI, that regex doesn't allow for whitespace or newlines in comments, which is often why people use that style of commenting.
It allows for whitespace. As for newlines, that depends on the default mode. "m" will stop at newlines, "s" will not.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

now this is my pattern
/((\/\*)(.+?)(\*\/))/s
right now, it just highights
/*
and
*/
not the stuff in the middle. help?
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

i got these type of comments working
// comment
#comment
but i still need help with
/*
comment
*/
here's my pattern

Code: Select all

$sql = preg_replace("'(\/\*(.*?)\*/|//[^<br />].*?$|\#[^<br />].*?$)'", "<span class='comment'>\\1</span>", $sql);
i think that problem is that inside the /* and */ are spans that highlight those words. how should i make it so that it doesn't highlight that part?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Well, you'd also have to run a regex on your comments to remove HTML tags within them if you don't want double highlighting.
Post Reply