Re: Verifying a certain amount of numbers
| From: | Curt Zirzow | Date: | Wed, 16 Jul 2003 15:23:23 +0000 |
| Subject: | Re: Verifying a certain amount of numbers | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-155446@lists.php.net to get a copy of this message | ||
Ford, Mike [LSS] <M.Ford@lmu.ac.uk> wrote:
> > -----Original Message-----
> > From: Curt Zirzow [mailto:curt@zirzow.dyndns.org]
> > Sent: 16 July 2003 15:17
> >
> > Justin French <justin@indent.com.au> wrote:
> > > If you care about performance at all, try and find away around the
> > > problem without regular expressions...
> > >
> > > I tested
> > >
> > > if( (strlen($str) == 6) && (is_int($str)) )
> > >
> > > vs
> > >
> > > if(ereg('^[0-9]{6}$',$str))
> > >
> > >
> > > ...on my LAN test server with 100000 iterations, and the regexp was
> > > nearly 2 times slower than the first solution. IMHO, it's
> > also faster
> >
> > Excellent point! I find my self using regex's a bit to often
> > when there
> > are other solutions available.
> >
> > btw, have you ever tested the difference between
> >
> > if(ereg('^[0-9]{6}$',$str))
> > if(preg_grep('^[0-9]{6}$',$str))
>
> The first will work and the second won't?
>
> Seriously, I think you mean:
>
> if (preg_match('/^[0-9]{6}$/', $str))
yea, I havn't used preg_* much, i wasn't aware you needed the //.
>
> Having said which, a quick run on my (rather ancient and slow Windows NT)
> system with the above tests and 1,000,000 iterations of each reveals:
>
> Succeeding ereg: 21.1589909792
> Succeeding preg_match(): 14.6125850677
>
> Failing ereg(): 21.2370660305
> Failing preg_match(): 13.5106118917
I wonder if preg treats [0-9] vs. \d differently:
if (preg_match('/^\d{6}$/', $str))
>
> Cheers!
>
> Mike
Thanks,
Curt.