有SQL:
select a,b,c,d from `tab` where a=1 and b=1 order by d desc
或
select * from `tab` order by d desc
或
select a,b,c,d from `tab` order by d desc
或
select a,b,c,d from `tab`如何通过正则替换后变成select count(1) from `tab` where a=1 and b=1 order by 主键 asc
或
select count(1) from `tab` order by 主键 asc
select a,b,c,d from `tab` where a=1 and b=1 order by d desc
或
select * from `tab` order by d desc
或
select a,b,c,d from `tab` order by d desc
或
select a,b,c,d from `tab`如何通过正则替换后变成select count(1) from `tab` where a=1 and b=1 order by 主键 asc
或
select count(1) from `tab` order by 主键 asc
//$str = "select a,b,c,d from `tab` where a=1 and b=1 order by d desc";
$str = "select * from `tab` order by d desc";
$f = array("/a,b,c,d/","/\*/","/`/","/ d /","/desc/");
$t = array('count(1)','count(1)','',' 主键 ','asc');
$str = preg_replace($f,$t,$str);
echo $str ;
select a,b,c,d from `tab` where a=1 and b=1 order by d desc
或
select * from `tab` order by d desc
或
select a,b,c,d from `tab` order by d desc
或
select a,b,c,d from `tab`红色部分是不定的
$str = "select a,b,c,d from `tab` order by d desc";
$str = "select a,b,c,d from `tab`";
$str = "select * from `tab`";
$f = array();
$t = array();
$str = str_replace('`','',$str);
preg_match('/select\s+(.*?)\s+from/i',$str,$match);
$match[1] == '*' ? $f[] = "/\\".$match[1]."/i" : $f[] = "/".$match[1]."/i" ;
$t[] = 'count(1)';
if(preg_match('/by\s+(\w+)\s+(asc|desc)/i',$str,$match)){
$f[] = "/ ".$match[1]." /i" ;
$f[] = "/".$match[2]."/i" ;
$t[] = ' 主键 ';
$t[] = 'asc';
}
$str = preg_replace($f,$t,$str);
echo $str ;