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 »

i just thought of a really good idea...

i use a preg_match_all on all text between /* and */
then i use foreach, and inside that, i use another preg_match_all that removes the html tags, which i posted the code for before.
would this work?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

That could work if you code it right...

On the topic of unorthodox methods, why not preg_match_all the tags themselves and str_replace them out of the original?

Code: Select all

preg_match_all('/\/\*.*?(<[^>]+>)/', $str, $matches);
foreach($matches[1] as $tag)
{
    str_replace($tag, '', $str);
}
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


ok i got this

Code: Select all

preg_match_all("'(/\*.*)(.*)(.*\*/)'", $sql, $matched, PREG_SET_ORDER);
	foreach($matched[2] as $stuff){
		preg_match_all("/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/", $stuff, $match, PREG_SET_ORDER);
		foreach($match as $var){
			$tagb = $var[1];
			$text = $var[3];
			$tage = $var[4];
			$sql = str_replace($tagb.$text.$tage, $text, $sql);
		}
	}
but when i try it, it says
invalid argument supplied for foreach()

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Which one?

Always make sure that you check isset() or is_array(), as well.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

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

Post by feyd »

Your expression is quite greedy.

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

Post by superdezign »

Lucky for you, I'm in the middle of making a parser, so I'm in a Regex kinda mood.

Code: Select all

preg_match_all('#/\*.*?\*/#', $str, $matches);
	
if(is_array($matches[0]))
{
	foreach($matches[0] as $comment)
	{
		$str	= str_replace($matches[0], preg_replace('#<([^>])>(.*?)</\1>#', '$2', $comment), $str);
	}
}
It works fine.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

By the way, I just thought of a way that this could have been pretty much solved using only CSS.

Code: Select all

.comment,
.comment * {
/* style */
}
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

simple solutions for complicated problems
i am sure i heard this before. :lol:
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

^_^

Funny, I didn't even think of it until I started writing a PHP parser and got to doing comments, and as I was writing the CSS I'm like "Ohh... he could have done this too."
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

i already made a parser which works really great. i am creating this so i can have it as an attachment for bbcode. :P
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

i got another problem
for the // and # type comments, it highlights everything but numbers, even with the css you gave me 2 posts ago. the number highlighting seems to end the the comment highlighting
here is my code

Code: Select all

$sql = preg_replace("'((//|#)(?:(\w|\s|\d))*[^<>])'is", "<span class='comment'>\\1</span>", $sql);
Last edited by ziggy3000 on Sun Jun 17, 2007 11:15 am, edited 1 time in total.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Whoa, you're making that pattern much harder than it is. Non-capturing isn't what you want at all because you need the entire comment. Also, the 's' modifier makes your code not stop at newlines, but '//' and '#' comments do stop at newlines. What you want is:

Code: Select all

$sql = preg_replace('|((//|#).*)|', '<span class="comment">$1</span>', $sql);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You have delimiter errors superdezign. It's also a good idea to make sure the pattern uses multi-line mode.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Hehe, I wasn't sure if '|' could be a delimiter, but I figured that if it couldn't, ziggy would let me know. :P

And... if he used multiline... would he have to check against a newline character?

Code: Select all

((//|#)[^\n]+?)
Post Reply