Created
April 13, 2014 07:42
-
-
Save aliharis/10573509 to your computer and use it in GitHub Desktop.
Format Telephone Number with PHP
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
<?php | |
function formatTelephoneNo($phone_number, $country_code = false) | |
{ | |
$cleaned = preg_replace('/[^[:digit:]]/', '', $phone_number); | |
if ($country_code) { | |
preg_match('/(\d{3})(\d{3})(\d{4})/', $cleaned, $matches); | |
return "({$matches[1]}) {$matches[2]} {$matches[3]}"; | |
} | |
preg_match('/(\d{3})(\d{4})/', $cleaned, $matches); | |
return "{$matches[1]} {$matches[2]}"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: With country code
echo formatTelephoneNo("+9603303033", true);
Result: (960) 330 3033
Usage: Without country code
echo formatTelephoneNo("3303033");
Result: 330 3033