MySQL highlighter
Moderator: General Moderators
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
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?
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);
}feyd | Please use
but when i try it, it says
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 thisCode: 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);
}
}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]- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Lucky for you, I'm in the middle of making a parser, so I'm in a Regex kinda mood.
It works fine.
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);
}
}- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
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 */
}- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
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
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.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
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);- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Hehe, I wasn't sure if '|' could be a delimiter, but I figured that if it couldn't, ziggy would let me know. 
And... if he used multiline... would he have to check against a newline character?
And... if he used multiline... would he have to check against a newline character?
Code: Select all
((//|#)[^\n]+?)