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 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号