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