匹配所有的sql语句,要求提取出select,from,where三个部分,order和group部分不要
select a.*,b.* from aaa a left join bbb b on a.id=b.id where a.id=33 group by a.id order by b.id
类似这样的语句 ,要求提取出 a.*,b.* 和 aaa a left join bbb b on a.id=b.id where a.id=33
...全文
3566打赏收藏
求一个匹配SQL语句的正则
匹配所有的sql语句,要求提取出select,from,where三个部分,order和group部分不要 select a.*,b.* from aaa a left join bbb b on a.id=b.id where a.id=33 group by a.id order by b.id 类似这样的语句 ,要求提取出 a.*,b.* 和 aaa a left join bbb b on a.id=b.id where a.id=33
<?php
$Str="select a.*,b.* from aaa a left join bbb b on a.id=b.id where x=\"xasdfas select saflsafj from asfsaf where x=\"xasdfas select saflsafj from asfsaf where x=\"xasdfas select saflsafj from asfsaf where safasf\"\" safasf\" a.id=33 group by a.id order by b.id ";
print preg_replace("/select(.+?)from(.+?)where(.*?)(order|group)/","Se: $1<br />Fro: $2<br /> Whe $3 <p />",$Str);
?>结果:
---------------
Se: a.*,b.*
Fro: aaa a left join bbb b on a.id=b.id
Whe x="xasdfas select saflsafj from asfsaf where x="xasdfas select saflsafj from asfsaf where x="xasdfas select saflsafj from asfsaf where safasf"" safasf" a.id=33
by b.id group by a.id
---------------
可包含无限子查询
换成
'order by b.id group by a.id'
'group by a.id order by b.id'
都没问题但~~
哪位高手给看看,我搞了半天'by b.id group by a.id'也没去掉.
这样不行,详细说一下吧,现在语句的可能性有
select .... from .....
select .... from .... order by ...
select .... from .... group by ... order by ...
select .... from .... where ....
select .... from .... where .... order by ...
select .... from .... where .... group by ... order by ...
主要的就是这些了吧,就是order by ... 部分不要 group by ...部分不要 然后有where的,join的 都包含到第二部分里边 select的部分算第一部分
<?php
$Str="select a.*,b.* from aaa a left join bbb b on a.id=b.id where a.id=33 group by a.id order by b.id";
echo preg_replace("/select (.+?) from (.+?) where (.+?) /i","$1<br />$2<br />$3<br />",$Str);
?>