Re: ereg Confusion
| From: | Jeff Warrington | Date: | Thu, 09 Nov 2000 10:40:20 +0000 |
| Subject: | Re: ereg Confusion | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-24604@lists.php.net to get a copy of this message | ||
In article <00c501c04827$69f60870$48063e0a@HPLOAN7>, alessio@mvfx.com
wrote:
Since you are using () you are storing the output of the match between
the (). You can access this match through a variable that you specify as
the third parameter of the eregi function call.
so,
$value = "ME-123456666";
if (ereg("ME-([0-9]{5})",$value,$regs)) {
$val = $regs[1];
}
print ("val => $val");
will yield:
val => 12345
if you used:
if (ereg("ME-([0-9]{5}[^0-9]),$value,$regs))
you would return false, since the minimum five followed by
non numbers would fail.
Jeff Warrington
> I need to validate user input for tags that are in the format of
> "ME-xxxxx" where 'x' is numeric. The input needs to start with
> "ME-" and
> then have 5 numbers. I created the following regular expression but I am
> not getting the results I would expect. It evaluates false if I change
> the "ME-" or put any non-numeric value after "ME-" but it still
> evaluates true if there are more than five numbers after "ME-".
>
> -- Evaluates TRUE
> $value = "ME-12345";
> ereg("ME-([0-9]{5,5})", $value)
>
> -- Evaluates FALSE
> $value = "ME-1X345";
> ereg("ME-([0-9]{5,5})", $value)
>
> -- Evaluates True
> $value = "ME-123456789012345678";
> ereg("ME-([0-9]{5,5})", $value)
>
> Am I missing something? This seems like it should produce the desired
> results.
>
> Config: PHP 4.0.2 FreeBSD 4.1 Apache 1.3.12
>
> Thanks.
>
> - Jamie
>
>