duanqiao1926 2012-03-02 22:07
浏览 45
已采纳

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

  • 写回答

1条回答 默认 最新

  • duanli9001 2012-03-03 09:43
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料