Last active
December 4, 2020 07:16
-
-
Save jefferyrdavis/5992271 to your computer and use it in GitHub Desktop.
Snippit: PHP: Format 10 or 7 digit phone number
This file contains 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 | |
/* formats a 10 or 7 digit phone number (number only) by adding dashes at the appropriate locations */ | |
function phoneFormat($number) { | |
if(ctype_digit($number) && strlen($number) == 10) { | |
$number = substr($number, 0, 3) .'-'. substr($number, 3, 3) .'-'. substr($number, 6); | |
} else { | |
if(ctype_digit($number) && strlen($number) == 7) { | |
$number = substr($number, 0, 3) .'-'. substr($number, 3, 4); | |
} | |
} | |
return $number; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment