0

as i say in title i'm learning how to make syntax highlighter to mysql code using regex in php

here is my code i just tried to do

$css    = '<style> body{font-family:tahoma;font-size:12px;}</style>';
$lista  = 'select|insert|update|delete|drop|truncate|alter' ;
$lista2 = 'into|from|values|desc|asc|on' ;
$lista3 = 'where|order by|limit|having|group by|union|left join|right join|full join|outer join|inner join' ;
$code   = preg_replace('/('.$lista.')/i','<br /><span style="color:#f00;">$1</span>',$this->query);
$code   = preg_replace('/('.$lista3.')/i','<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0f0">$1</span>',$code);
$code   = preg_replace('/('.$lista2.')/i','<span style="color:#00f">$1</span>',$code).$css;

my result was like this img

syntax result here

i want to use simple way to do this

but i shouldn't use any plugin just regex to understand it too

Toto
  • 89,455
  • 62
  • 89
  • 125
Mona Abdelmajeed
  • 686
  • 1
  • 9
  • 19
  • @Billy Moon , thank you very much , can you tell me how and why ?? -> learning :) – Mona Abdelmajeed Mar 02 '12 at 22:23
  • There are many questions about this on stack overflow, but this one seems to hit the nail on the head: http://stackoverflow.com/questions/5389244/building-a-regex-based-parser – Billy Moon Mar 03 '12 at 08:37

1 Answers1

0

I agree with Billy Moon about a parser, but if you really want to use regex, yours is close to work, you've just forgotten word boundaries

$code   = preg_replace('/\b('.$lista.')\b/i','...',$this->query);
$code   = preg_replace('/\b('.$lista3.')\b/i','...',$code);
$code   = preg_replace('/\b('.$lista2.')\b/i','...', $code);
Toto
  • 89,455
  • 62
  • 89
  • 125