$sql = "select id,fid from tableName where id > 100 order by id asc limt 10"function getCounts($sql) { /* 我想让这个函数经过正则处理之后返回 select count(*) from tableName; */ }
不好意思我是楼主,刚才说错了补充一下, 经过函数getCounts处理之后返回 select count(*) from tableName where id > 100 order by id asc limt 10
我再补充一下,谢谢不发意思,;/* $sql = "select id,fid from tableName where id in(select id from bb)order by id asc limt 10" 经过函数getCounts处理之后返回 select count(*) from tableName where id in(select id from bb)order by id asc limt 10 */
preg_replace("/\s*select\s.*\sfrom\s/i","select count(*) from ",$sql,1);
preg_replace("/($|[^[a-z])select($|[^[a-z]).*($|[^[a-z])from($|[^[a-z])/i","select count(*) from ",$sql,1);
看起来就是要将select的字段序列换成count(*),但子查询中的不变? $sql="select id,fid from tableName where id in(select id,fid from bb)order by id asc limt 10";$str = preg_replace('/select\s([^\s]+)/is','select count(*)',$sql,1);echo $str;output: select count(*) from tableName where id in(select id,fid from bb)order by id asc limt 10
经过函数getCounts处理之后返回
select count(*) from tableName where id > 100 order by id asc limt 10
$sql = "select id,fid from tableName where id in(select id from bb)order by id asc limt 10"
经过函数getCounts处理之后返回
select count(*) from tableName where id in(select id from bb)order by id asc limt 10
*/
/*
下在是我所能想到的正则:但是感觉有些特殊情况,不太强壮,
麻烦正则高手朋友指点一下,谢谢了
echo preg_replace("/\s*?select.*?from/i","select count(*) from",$sql,1);*/
$sql="select id,fid from tableName where id in(select id,fid from bb)order by id asc limt 10";$str = preg_replace('/select\s([^\s]+)/is','select count(*)',$sql,1);echo $str;output:
select count(*) from tableName where id in(select id,fid from bb)order by id asc limt 10