On Wed, Dec 29, 2010 at 19:12, Ethan Rosenberg <[email protected]> wrote:
> Dear List -
>
> Thank you for all your help in the past.
>
> Here is another one....
>
> I would like to have a regex which would validate that a telephone number
> is in the format xxx-xxx-xxxx.
Congrats. People in Hell would like ice water. Now we all know
that everyone wants something. ;-P
Really, this isn't a PHP question, but rather one of regular
expressions. That said, something like this (untested) should work:
<?php
$numbers = array(
'123-456-7890',
'2-654-06547',
'sf34-asdf-',
'abc-def-ghij',
'555_555_5555',
'000-000-0000',
'8007396325',
'241-555-2091',
'800-555-0129',
'900-976-739',
'5352-342=452',
'200-200-2000',
);
foreach ($numbers as $n) {
echo $n.(validate_phone($n) ? ' is ' : ' is not ').'a valid
US/Canadian telephone number.'.PHP_EOL;
}
function validate_phone($number) {
if
(preg_match('/^[2-9]{1,}[0-9]{2,}\-[2-9]{1,}[0-9]{2,}\-[0-9]{4,}$/',trim($number)))
{
return true;
}
return false;
}
?>
--
</Daniel P. Brown>
Dedicated Servers, Cloud and Cloud Hybrid Solutions, VPS, Hosting
(866-) 725-4321
http://www.parasane.net/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php