Re: Regex for telephone numbers

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Dec 29, 2010 at 19:12, Ethan Rosenberg <ethros@xxxxxxxxxxxxx> 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




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux