Skip to content

Instantly share code, notes, and snippets.

@mpezzi
Created August 25, 2011 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mpezzi/1171581 to your computer and use it in GitHub Desktop.
Save mpezzi/1171581 to your computer and use it in GitHub Desktop.
PHP Validate Phone Number
<?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