Skip to content

Instantly share code, notes, and snippets.

@commix
Forked from mpezzi/gist:1171581
Created October 2, 2012 18:21
Show Gist options
  • Save commix/3822015 to your computer and use it in GitHub Desktop.
Save commix/3822015 to your computer and use it in GitHub Desktop.
PHP: Validate - Phone Number
/**
* 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