Created
December 16, 2013 22:52
-
-
Save sptrsn/7996038 to your computer and use it in GitHub Desktop.
Phone Number Format
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
//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