Skip to content

Instantly share code, notes, and snippets.

@sptrsn
Created December 16, 2013 22:52
Show Gist options
  • Save sptrsn/7996038 to your computer and use it in GitHub Desktop.
Save sptrsn/7996038 to your computer and use it in GitHub Desktop.
Phone Number Format
//strip all non-numeric characters.
//requires exactly 10 digits
//returns a hyphenated phone number
function formatPhone($number){
$number = preg_replace("/[^0-9]/", "", $number);
if (strlen($number) == 10){
$area = substr($number, 0, 3);
$part1 = substr($number, 3, 3);
$part2 = substr($number, 6);
return "$area-$part1-$part2";
} else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment