Skip to content

Instantly share code, notes, and snippets.

@jefferyrdavis
Last active December 4, 2020 07:16
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jefferyrdavis/5992271 to your computer and use it in GitHub Desktop.
Save jefferyrdavis/5992271 to your computer and use it in GitHub Desktop.
Snippit: PHP: Format 10 or 7 digit phone number
<?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