PHP REGEX:学习如何格式化mysql查询(如jquery的语法highligher插件)

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

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);