By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,705 Members | 1,371 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,705 IT Pros & Developers. It's quick & easy.

regexp phone # check

P: n/a
I'm working on validating US phone numbers. I have a nice expression that
Regex Coach likes, but causes PHP to reject everything I send. Are there
any glaring differences? I can't figure out what's wrong. Another little
email check works fine, using the code out of Wrox' Beginning PHP.

PhoneCheck.php
<?php
function PhoneCheck($number)
{
return ereg("^[(\[]?\d{3}[)-\.\] ]*\d{3}[-\. ]?\d{4}$", $number,
$scrap);
}
?>

My test cases (which passed regex coach, but failed my php form):
123 456 7890
(123) 456-7890
123.456.7890
123-456-7890

Thanks,
-G
Jul 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Greg Bryant wrote:
I'm working on validating US phone numbers. I have a nice expression that
Regex Coach likes, but causes PHP to reject everything I send. Are there
any glaring differences? I can't figure out what's wrong. Another little
email check works fine, using the code out of Wrox' Beginning PHP.


try preg_match() instead of ereg()

and don't forget the regex delimiters
<?php
function PhoneCheck($number)
{
return preg_match("/^[(\[]?\d{3}[)-\.\] ]*\d{3}[-\. ]?\d{4}$/", $number, $scrap);
}
?>

--
..sig
Jul 17 '05 #2

P: n/a
Pedro Graca <he****@hotpop.com> wrote in
news:bo*************@ID-203069.news.uni-berlin.de:
try preg_match() instead of ereg()

and don't forget the regex delimiters
<?php
function PhoneCheck($number)
{
return preg_match("/^[(\[]?\d{3}[)-\.\] ]*\d{3}[-\. ]?\d{4}$/",
$number, $scrap);
}
?>


Thanks!
Jul 17 '05 #3

P: n/a
On Sun, 09 Nov 2003 01:34:18 GMT, Greg Bryant <br**********@yahoo.com> wrote:
return ereg("^[(\[]?\d{3}[)-\.\] ]*\d{3}[-\. ]?\d{4}$", $number,
$scrap);


(Someone else has already pointed out to use preg_match instead of ereg - \d
doesn't work in ereg)

Put regexes inside single quotes instead of double quotes - it saves you some
escaping.

Inside double quotes, "\." is the same as ".", so it gets to the regex as a
"." - match any character.

Inside single quotes, '\.' stays as '\.', so gets to the regex as '\.' - match
a literal '.' character.

You don't need to escape . inside a character class anyway, so in this case it
didnt make a difference.

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #4

P: n/a
Andy Hassall, obviously a huge fan of Grandmaster Flash, wrote:

Inside double quotes, "\." is the same as ".", so it gets to the regex as a
"." - match any character.


Hmm, not in my experience...

fnord@demogorgon:~$ cat preg.php
<?php
if(preg_match("'^\.$'", "."))
echo "\"'^\\.$'\" matches \".\".\n";
if(preg_match("'^\.$'", "x"))
echo "\"'^\\.$'\" matches \"x\".\n";
?>
fnord@demogorgon:~$ php4 -q preg.php
"'^\.$'" matches ".".
fnord@demogorgon:~$

/joe
--
In the Rich building, the decompiler is pernicious. In the beer fridge, the
processor is fanatic... The fantastic anus is 2x-ice-cold.
Jul 17 '05 #5

P: n/a
Andy Hassall wrote:
Put regexes inside single quotes instead of double quotes - it saves you some
escaping.
Well, it certainly saves parsing the string for variables. But
perhaps that may be desired from time to time.
Inside double quotes, "\." is the same as ".", so it gets to the regex as a
"." - match any character.
I don't think so. A slash followed by a period is understood to
mean a literal period in a regular expression, whether it's within
single- or double-quotes, or within a character class.

$string = 'abc';
echo preg_match("`ab(?=\.)`",$string) ? 'Match' : 'No match';
echo preg_match('`ab(?=\.)`',$string) ? 'Match' : 'No match';

Moreover, in any single- or double-quoted string, a period
following a backslash is treated literally -- that is, "\.".

http://www.php.net/manual/en/language.types.string.php
You don't need to escape . inside a character class anyway, so in this case it
didnt make a difference.


Right.

I'm not familiar with the format(s) of US phone numbers, but, from
the test cases provided, I suspect the hyphen must be escaped. If
it isn't, the characters ")", "*", "+", ",", "-", ".", " ", and
"]" will be permitted by the second character class (I.e., those
of the ASCII repertoire between RIGHT PARENTHESIS and FULL STOP
inclusive, and the RIGHT SQUARE BRACKET and SPACE).

Hmm. Is "(999]...]) ]---]999 9999" really a proper national
phone number?

--
Jock
Jul 17 '05 #6

P: n/a
On Sun, 9 Nov 2003 16:03:10 +0000 (UTC), Disco Plumber <sc**@moralminority.org>
wrote:
Andy Hassall, obviously a huge fan of Grandmaster Flash, wrote:

Inside double quotes, "\." is the same as ".", so it gets to the regex as a
"." - match any character.


Hmm, not in my experience...

fnord@demogorgon:~$ cat preg.php
<?php
if(preg_match("'^\.$'", "."))
echo "\"'^\\.$'\" matches \".\".\n";
if(preg_match("'^\.$'", "x"))
echo "\"'^\\.$'\" matches \"x\".\n";
?>
fnord@demogorgon:~$ php4 -q preg.php
"'^\.$'" matches ".".
fnord@demogorgon:~$


Whoops - my mistake.

So ignore all my post except the bit about not needing to escape . in a
character class.

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #7

P: n/a
Andy Hassall (34.365% quality rating):

Whoops - my mistake.
So ignore all my post except the bit about not needing to escape . in a
character class.


I actually mentioned it to my friend, and he agreed with what you said,
but then I showed him the code. He said it might be a version difference.
For reference, the previous post was run with PHP 4.1.2.

/joe
--
A Playstation is long. The case from Freebeer will go to El Myr!
Jul 17 '05 #8

P: n/a
Andy Hassall (97.635% quality rating):

So ignore all my post except the bit about not needing to escape . in a
character class.


And you know, I actually didn't realize that, but it makes perfect
sense. Having a match-all character in a character class would be silly.

/joe
--
In the Atlanta Diner, Nick Ralabate barely felches the Student Center for
the anus, and then interestingly memorizes the configuration of Jon
Beckham's T1? In 'Narz!, Pizza says stupid shit about Q, and then triply,
cleverly, doubly jacks into David Wada's sig generator f... [tape runs out]
Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.