views:

33

answers:

2

Hi there,

I'm looking for a way of determining whether a phone number is in the international format or a local format, and if it's in the international format what country it belongs to. If it's in the local format it should prompt the user for the country code and then convert it into the international format.

Examples:

  • 00447749123123 = international UK number - reformat to 447749123123

  • +447749123123 = international UK number - reformat to 447749123123

  • 447749123123 = international UK number

  • 07749123123 = national UK number - prompt for country code and reformat to 447749123123

  • 18775784000 = international american number

  • 8775784000 = national american number - prompt for country code and reformat to 18775784000

Does anyone know if such a script exists, or if not how to go about doing this? IT should work for as many countries as possible (not just the UK and US).

A: 

I don't think such a script or web service exists, I researched something similar a couple of weeks back.

As various countries have different length phone numbers I think you'll have a hard time creating a script that works for all countries with all different ways of writing a phone number.

You have to make assumptions about the phone number given as input. Basically you have two different approaches.

  1. Make a rigid solution that only works with known domestic phone number formats
  2. Make a flexible solution that only accepts easily identifiable international formats, but as a consequence, accepts any domestic format.

I'd go for option number two.

In your examples number 1 and 2 are easily identifiable as international numbers. Number 3 is hard to identify, because it could either be an international UK number or an unknown country with 12 digit domestic phone numbers.

So I'd start with accepting option one and two as valid international numbers and skip number three.

<?php
$number = "00447749123123"; // remember the quotes the number must 
                            // keep leading zeros.

if (preg_match('/^(00|\+)/', $number)) {
    echo "International number";
    // parse and recognize country code.
} else {
    echo "national number";
    // identify which country
}

Identifying national phone numbers without knowing which country it is in advance is impossible since many countries will share the same formats. Having users choose their country in a webform is therefore neccessary to add the international code to the numbers.

thomasmalt
A: 

There is nothing free but Neustar would be your best paid service. I think they offer international number services as well but you might have to code your own API interface. Another paid route might be Twilio, but I'm not sure of their international options. I know some of the larger SMS Aggregators might offer this for their products but not as a stand alone API/service. If you're looking for a paid option you might try OpenMarket (MxTelecom for international). Just a thought

Phill Pafford