日期:2014-05-18 浏览次数:20666 次
$str="SELECT u.id,u.name FROM user as u WHERE u.id=12 ORDER BY u.id DESC limit 10"; echo preg_replace("/^(select ).+? (from .+?)(limit (\d{1,}(\,\d{1,})?))?$/i","\\1 count(*) \\2 limit 1;",$str);
------解决方案--------------------
<?php
$str="SELECT u.id,u.name FROM user as u WHERE u.id=12 ORDER BY u.id DESC limit 1,10";
echo preg_replace("/^(select ).+ (from.+?)( limit [0-9\,]+){0,1}$/i","\\1 count(*) \\2 limit 1",$str);
echo "<br />";
$str="SELECT u.id,u.name FROM user as u WHERE u.id=12 ORDER BY u.id DESC";
echo preg_replace("/^(select ).+ (from.+?)( limit [0-9\,]+){0,1}$/i","\\1 count(*) \\2 limit 1",$str);
?>