Using PHP to format phone numbers

PHP is great. It complements JavaScript wonderfully. You may have already formatted the phone number on the front end, but what about on the backend? There comes a time when you need to use php to format a phone number (php format phone number). I use this function in my projects all the time.

PHP Format Phone Number Function

How to use php format telephone number:

function format_phone($country, $phone) {
$function = 'format_phone_' . $country;
if(function_exists($function)) {
return $function($phone);
}
return $phone;
}
function format_phone_us($phone) {
// note: making sure we have something
if(!isset($phone{3})) { return ''; }
// note: strip out everything but numbers 
$phone = preg_replace("/[^0-9]/", "", $phone);
$length = strlen($phone);
switch($length) {
case 7:
return preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $phone);
break;
case 10:
return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $phone);
break;
case 11:
return preg_replace("/([0-9]{1})([0-9]{3})([0-9]{3})([0-9]{4})/", "$1($2) $3-$4", $phone);
break;
default:
return $phone;
break;
}
}
// usage
$phone = '111 111 1111';
$phone = format_phone('us', $phone);
echo $phone;

You might be wondering why I have 2 functions in there, right? Well, here’s the thing. I like to make things resusable. That being said, I’m leaving the door open to use the same kind of interface, format_phone(‘us’, $phone) or format_phone(‘canada’, $phone), to format other types of phone numbers should the functions exists. I’ll leave those types of implementations as homework.

What did we learn today?

We, well, mostly you since I wrote this right? You just learned how to “php format phone.” Use with caution, but use it. If you are an agency or you need another PHP developer by your side, please contact The Barton Organization today for php development work.

Thanks for learning about php telephone number validation.

  1. Dave Shaffer Reply

    This was really useful!

    I ended up using this in OpenCart’s abstract controller class so it would be available to all templates. To make this work I changed a couple things:

    1. I changed the `if(function_exists($function)) {` line to `if (method_exists($this, $function)) {`
    2. I changed the method signature to `function format_phone($phone, $country = ‘us’) {` since that’s a useful default for my application

    Thanks!

  2. neotechni Reply

    ” Well, here’s the thing. I like to make things resusable. ”

    I love you. Far too many programmers don’t do that. Even the PHP example site doesn’t make their code reusable.

    BTW: I used the same 2 function approach for sending SMS messages (instead of country, it was the SMS service provider)

  3. Dan Reply

    For what it’s worth, writing code today to make code reusable down the road violates the very heart of modern agile development methodology, particularly YAGNI. Google that if this sparks your curiosity.

    • The Barton Reply

      Thanks Dan. We’re not all going to agree on everything in life. I agree to a point. However, it still makes sense to write code that is flexible, which is kind of what I was going for with this short example. But you’re right, it wouldn’t make sense for me to write a another phone format function in PHP (or .NET) because some people would say “you aren’t gonna need it.”

  4. Parker Reply

    An example of how to do things. Not a lot of verbiage, just the facts, ma’m.

    Nice!

Leave a comment

Find the Best SEO in Fort Lauderdale, Florida (Plantation SEO) | Drupal & Worpress Develpment