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 »

Well, if you had Regex Coach, you would. :P
Just get it already.


$2 and $5 are the HTML tags.
$1, $4, and $6 is everything else.

Soo.. You preg_replace the string with $1, $4, and $6.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

here's something that i have told you a long time ago...

I GOT REGEX COACH A LONG TIME AGO!!!!
THAT JUST TELLS YOU WHAT TO USE AS REGEX, NOT HOW TO USE IT
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Don't get impatient with anyone Ziggy3000. :?
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

it's not that, i have told him a lot of times, but in every post, he says regex coach about 3 times, and tells me to get it every time.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

superdezign has told you the matches you should be using, so whats the problem?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

ziggy3000 wrote:it's not that, i have told him a lot of times, but in every post, he says regex coach about 3 times, and tells me to get it every time.
That is understandable Ziggy. But you gotta remember that he is offering his time to help you. Yes, you did say once (that I can see in reviewing this thread) that you got the Regex Coach tool. But in almost that same post you continue to ask questions about not understanding what you are doing.

Maybe it is time, since you want to learn regular expressions as bad as you say, that you go pick up a book and do some reading instead of slapping down all the suggestions that are being offered to you throughout this thread. And as a suggestion, if you wanted to learn how to handle the regular expressions needed to produce a highlighter, why haven't you downloaded a functioning app and read that? It would be a lot more helpful to you than trying to have this community repeat itself every third post in order to try to help you.
ReDucTor
Forum Commoner
Posts: 90
Joined: Thu Aug 15, 2002 6:13 am

Post by ReDucTor »

Code: Select all

$str = 'SELECT * FROM blah';
$tokens = array();
$pos = 0;
for($i=0;$e=strlen($str);$i<$e;$i++) {
  if(strpos(' .,()[]',$str{$i}) !== false) {
    $tokens[++$pos] = $str{$i};
    $pos++;
  }
  if($str{$i} == '\'') {
    for(;$i<$e;$i++) {
      if($str{$i}=='\'' && $str{$i-1} != '\\') {
        $tokens[$pos] .= $str{$i}
        break;
      }
      $tokens[$pos] .= $str{$i};
   }
    $pos++;
  }
  if($str{$i} == '"') {
    for(;$i<$e;$i++) {
      if($str{$i}=='"' && $str{$i-1} != '\\') {
        $tokens[$pos] .= $str{$i}
        break;
      }
      $tokens[$pos] .= $str{$i};
   }
    $pos++;
  }
}

$highlight = array(
'SELECT' => 'red',
'UPDATE' => 'green',
'DELETE' => 'blue'
'FROM' => 'purple',
'WHERE'=>'pink'
);

foreach($tokens as $token) {
if(isset($highlight[$token]))
print '<font color="'.$highlight[$token].'">'.$token.'</font>';
else if($token{$i} == '\'' || $token{$i} == '"')
print '<font color="orange">'.$token.'</font>';
else
print $token;
}
Yes, I know its retarded. But you know your [s]gunna[/s] going to try it, [s]coz[/s] because i didnt try it. So theres probably some mistake in it.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

ziggy3000 wrote:here's something that i have told you a long time ago...

I GOT REGEX COACH A LONG TIME AGO!!!!
THAT JUST TELLS YOU WHAT TO USE AS REGEX, NOT HOW TO USE IT
It just makes it easier take it step-by-step. If you have it, why aren't you using it? And if you are using it, why haven't you solved your problem by now??

It seems to me as though you hadn't even tried since the last time I gave you suggestions. That doesn't mean that you haven't, but it does mean that you haven't shown any evidence that you have. If I really wanted to, I could write all of your Regex for you, but I feel that I've done too much. You say you want to learn it, so please do. Every time that I gave you code was to teach myself, but it doesn't do anything to help you learn it.

I'd really suggest you look in the Regex forum here at the "Crash Courses." I learned almost everything that I know from them. Very well-organized.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

ok, i am using this for multiline comments

Code: Select all

$sql = preg_replace("'(/\*.*)(<([\w]+)>)(.*)(</\\3*>)(.*\*/)'", "<span class='comment'>$1$4$6</span>", $sql);
it still doesn't remove html tags.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Call a preg_match_all and output the matches. See what you get.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

it's not have any matches...
Array ( )
i am trying to parse this
/* This selects a database*/
select gets highlighted blue
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

but when i am outputting the matches with this, it output's it correctly.

Code: Select all

preg_match_all("/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/", $sql, $match, PREG_SET_ORDER);
i am gonna work on this part more
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

when i try this

Code: Select all

preg_match_all("/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/", $sql, $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);
	}
it removes the html tags. but when i add the comment tags to the preg_match, it doesn't work. it echos out unknown modifier "\"
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

You're probably escapint the comment characters incorrectly. What did you try?
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

i tried

Code: Select all

preg_match_all("/(/\*.*)(<([\w]+)[^>]*>)(.*)(<\/\\3>)(.*\*/)/", $sql, $match, PREG_SET_ORDER);
Post Reply