Skip to content

Instantly share code, notes, and snippets.

@cam-gists
Created November 28, 2012 21:33
Show Gist options
  • Save cam-gists/4164737 to your computer and use it in GitHub Desktop.
Save cam-gists/4164737 to your computer and use it in GitHub Desktop.
PHP: format Phone Number
function format_phone($phone)
{
$phone = preg_replace("/[^0-9]/", "", $phone);
if(strlen($phone) == 7)
return preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $phone);
elseif(strlen($phone) == 10)
return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $phone);
else
return $phone;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment