Skip to content

Instantly share code, notes, and snippets.

@ryanburnette
Last active March 25, 2017 00:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanburnette/3996858 to your computer and use it in GitHub Desktop.
Save ryanburnette/3996858 to your computer and use it in GitHub Desktop.
Format Phone Numbers
<?php
function format_phone_number($phone) {
$phone = preg_replace("/[^0-9]/", "", $phone);
if ( strlen($phone) == 7 ) {
return preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $phone);
}
if ( strlen($phone) == 10 ) {
return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $phone);
}
return $phone;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment