Created
August 25, 2011 19:29
-
-
Save mpezzi/1171581 to your computer and use it in GitHub Desktop.
PHP Validate Phone Number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Validate a phone number. | |
* | |
* @param $value | |
* A phone number as string. | |
* @param $formats | |
* The formats to validate for. | |
* @return | |
* TRUE or FALSE if it validates. | |
*/ | |
function valid_phone_number($value, $formats = NULL) { | |
if ( is_null($formats) ) { | |
$formats = array( | |
'##########', | |
'###########', | |
'###-###-####', | |
'#-###-###-####', | |
'### ### ####', | |
'# ### ### ####', | |
'(###) ###-####', | |
'# (###) ###-####', | |
'(###) ### ####', | |
'# (###) ### ####', | |
'####-###-###', | |
'(###) ###-###', | |
'####-####-####', | |
'##-###-####-####', | |
'####-####', | |
'###-###-###', | |
'#####-###-###', | |
); | |
} | |
return in_array(trim(preg_replace("/[0-9]/", "#", $value)), $formats); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment