RE: [PHP] OT - Regular Expression
| From: | Edward Kay | Date: | Fri, 09 Feb 2007 12:56:32 +0000 |
| Subject: | RE: [PHP] OT - Regular Expression | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-248644@lists.php.net to get a copy of this message | ||
> [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.
Edward