Re: Small regex help?
| From: | Guy Brom | Date: | Thu, 13 Oct 2005 09:50:14 +0000 |
| Subject: | Re: Small regex help? | ||
| References: | 1 2 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-21435@lists.php.net to get a copy of this message | ||
Worked great! here it is:
function rewriteCountQuery($sql)
{
if (preg_match('/^\s*SELECT\s+\bDISTINCT\b/is', $sql) ||
preg_match('/\s+GROUP\s+BY\s+/is', $sql)) {
return false;
}
$queryCount = preg_replace('/(?:.*)\bFROM\b\s+/Uims', 'SELECT COUNT(*)
FROM ', $sql, 1);
list($queryCount, ) = preg_split('/\s+ORDER\s+BY\s+/is', $queryCount);
list($queryCount, ) = preg_split('/\bLIMIT\b/is', $queryCount);
return trim($queryCount);
}
"Lorenzo Alberton" <l.alberton@quipo.it> wrote in message
news:434E163E.3090505@quipo.it...
> Guy Brom wrote:
>> Can anyone suggest the correct regex to replace col1,col2... with
>> count(*) and strip out everything just before ORDER BY?
>>
>> so for this:
>> SELECT col1,col2... FROM tbl WHERE filter1 filter2 ORDER BY order1,order2
>>
>> I would get this:
>> SELECT count(*) FROM tbl WHERE filter1 filter2
>
> have a look at rewriteCountQuery() in the Pager_Wrapper file:
>
> http://cvs.php.net/co.php/pear/Pager/examples/Pager_Wrapper.php
>
> HTH
>
> Regards,
> --
> Lorenzo Alberton
> http://pear.php.net/user/quipo