On Fri, 2007-02-09 at 12:56 +0000, Edward Kay wrote:
> > [snip]
> >
> > Why not use two checks? One that checks for 4 digits, the other checks
> > for existence of 8 anywhere in the string?
> >
> > Cheers,
> > Rob.
> > [/snip]
> >
> > Of course that is a possibility. But I wonder if there is a way to do it
> > with ONE expression. So there are no AND operator in Regular Expressions
> > then I assume.
> >
> > /^([0-9]{4}&[0-9]{0,3}8[0-9]{0,3})$/
> >
> > That would be nice :) But now there are no AND operator :*(
> >
> > /Peter
>
> 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.
--
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting |
| a powerful, scalable system for accessing system services |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for |
| creating re-usable components quickly and easily. |
`------------------------------------------------------------'
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php