Skip to content

Instantly share code, notes, and snippets.

@rob1121
Created July 21, 2016 02:11
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 rob1121/d0891d5be5db9fad32bba9d3d99c6a42 to your computer and use it in GitHub Desktop.
Save rob1121/d0891d5be5db9fad32bba9d3d99c6a42 to your computer and use it in GitHub Desktop.
using 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