Re: Regular expression. What is wrong?
| From: | Richard Lynch | Date: | Mon, 14 Feb 2005 17:25:15 +0000 |
| Subject: | Re: Regular expression. What is wrong? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-208658@lists.php.net to get a copy of this message | ||
Kostyantyn Shakhov wrote:
> I have to check the phone number. I use some regular expression for
> this. The phone number can contain only numbers and characters like
> +,-,),( and space. The problem is when I use a Perl-style all works as
> intended but when I use a Posix-style I've got the Warning: ereg():
> REG_ERANGE and the result is opposite to intended one. What is wrong
> in the following?
>
> <?php
> $test_phone = "8 (044) 419-0567";
>
> if(preg_match("/^[+]?[\d\-\040\)\(]+$/", $test_phone))
For starters, those \ characters are probably not doing what you think...
\ is special in PHP inside quotes (and apostrophes)
\ is also special to preg_match.
While \d might be fine today, because \d isn't a special combination to
PHP, you'd be safer to write \\d so that PHP will ALWAYS send \d to the
preg engine.
> {
> echo "valid phone<br />";
> }
> else
> {
> echo "not valid phone<br />";
> }
>
> if(ereg("^[+]?[0-9\-[:space:]\)\(]+$", $test_phone))
Here, the \- is out of whack.
If you want a literal - to be valid, put it at the end of your character
class [0-9-] for example, allows 0-9 and the '-'
[a-zA-Z0-9-] allows alphanumeric and the '-'
I don't think \- is kosher, though, which is your REG_ERANGE error.
You may want to check out "Regex Coach" -- a program that lets you try out
Regular Expressions and see them "in action" so to speak.
> {
> echo "valid phone<br />";
> }
> else
> {
> echo "not valid phone<br />";
> }
> ?>
>
> --
> Best regards,
> Kostyantyn Shakhov mailto:shakhov@fresh-design.net
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
Like Music?
http://l-i-e.com/artists.htm