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

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

Post by superdezign »

Ooh. That wouldn't include the <br /> tag. Making progress. :-p

I'd never heard of back-referencing. At first glance, I thought it just copied the pattern to avoid too much typing, but it actually looks for the same content as the sub pattern. That's interesting.

And yeah, preg_match_all() definitely seems to be the function that you're after to get all of the HTML tags.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

yea, but i can't get my pattern to work. i am adding one before and after the one on php.net which has the /* and */. can you use your regex coach(i dont know how to use it) and then tell me the pattern i need?

edit:
this is what i am using, but it doesn't work

Code: Select all

$sql = preg_replace("#(\/\*[^<>]*)(<([\w]+)[^>]*>)(.*)(<\/\\2>)(.*\*/)#", "<span class='comment'>\\1\\3\\5</span>", $sql);
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

I'm in the middle of converting my PHP4 classes to PHP5. PHP5 feels a lot more like C++. ^_^ Melikes.

Get Regex Coach, type the string you want to find a part of in the bottom box, and type in your regex piece by piece. You can see errors quickly.

If it's highlighted in yellow, its the whole pattern (return as the first element of the preg_match array), and you can highlight a sub pattern using the radio buttons below the string boxes. They'll correspond to the different sup patterns (1, 2, 3, etc.).


It's easy to just jump into because it makes sense. Start with simple regex and move into more complex regex.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

ok, using regex coach, i got the following regex

Code: Select all

(\/\*[^<>]*)(<([\w]+)[^>]*>)(.*)(</[\w]*>)([^<>]\*/)
how would i use it?
$1, $3 and $5 are the things i need to highlight, right?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

$1, $3, and $5 are the portions that you want to keep. Either preg_replace the rest out, or preg_match it and put it back together.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

well, i am trying

Code: Select all

$sql = preg_replace("#(\/\*[^<>]*)(<([\w]+)[^>]*>)(.*)(<\/\\3*>)([^<>]\*/)#", "<span class='comment'>\\1\\3\\5</span>", $sql);
but's it doesn't work.
ediT:
i am also trying this but it wont work

Code: Select all

preg_match_all("#(\/\*[^<>]*)(<([\w]+)[^>]*>)(.*)(<\/\\3*>)([^<>]\*/)#", $sql, $match, PREG_SET_ORDER);
	foreach($match as $var){
		echo $var[1].$var[4].$var[6];
	}
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Well, I can't really help you with "it won't work." You've gotta show me the result of the regex for me to know what's happening.\
(Heading to bed, so hopefully someone responds... If not, I'll help out in the morning.)
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

well, by it wont work, i meant that it wont remove the html tags so the result is the same as before.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

SOMEONE PLEASE HELP ME!!!!!!!!!!!!!!!!
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

I still need to see the regex and the result of it. We aren't magicians. :P
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

this is what i am trying

Code: Select all

preg_match_all("#(\/\*[^<>]*)(<([\w]+)[^>]*>)(.*)(<\/\\3*>)([^<>]\*/)#", $sql, $match, PREG_SET_ORDER);
	foreach($match as $var){
		echo $var[1].$var[4].$var[6]."<br />";
	}
and the result is that it highlights everything between /* and */, but if there is html tags between it, it wont highlight that.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

I think you'll need to restart that regex, because you're right, it doesn't work.

Did you get Regex Coach yet? I'm playing around with this in it and I've already ran into a little speedbump... That when a comment is open, regardless of whether or not it closes, it will still comment everything after it. Are you sure you can't just use a pre-made parser? :P
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

i can, but i want to learn what i am doing wrong.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Regex Coach will show you what you're doing wrong AS you're doing it. That's why I use it.


This works in Regex Coach when the comments are closed.

Code: Select all

(/\*.*)(<([\w]+)>)(.*)(</\3*>)(.*\*/)
You'll need to escape a few characters to bring it over to PHP.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

ok, but i dont get how to implement the regex so that it leaves out the html tags.
Post Reply