RE: OT - Regular Expression

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> > As Rob suggested, why not just use two checks? e.g.
> >
> > if ( (strlen($input) == 4) && (strpos($input, '8') !== FALSE ) ) {
> > 	// OK
> > } else {
> > 	// Not OK
> > }
> >
> > Not only is this logic much easier to understand than a regexp
> - important
> > when someone else has to maintain your code later on etc., I
> also believe it
> > will be faster than using preg_match.
>
> Those would be the wrong two checks, you'd need 3 checks to do it that
> way, I still suggested using a regex. The reason being that the original
> requirements need the entire input to be digits :)
>
> <?php
>
> if( ereg( '^[[:digit:]]{4}$', $input )
>     &&
>     strpos( $input, '8' ) )
> {
>     // woot!
> }
>
> // Or alternatively...
>
> if( strlen( $input ) == 4
>     &&
>     strpos( $input, '8' )
>     &&
>     ctype_digit( $input ) )
> {
>     // woot!
> }
>
> ?>
>
> Cheers,
> Rob.

Oops - good catch!

You still need the !== FALSE test with strpos through. Without it, the test
will fail for any string starting with 8 as strpos will return 0.

Edward

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux