tags:

views:

256

answers:

3

For a decent and standardized php function I want to format a user inputted string to a std. international phone number format. The string will be stripped from other characters as digits and +-signs. All spaces will be deleted. After that I want to format the different strings to a standardized form:

+310612345678  -> +31 (0)61 234 5678
00310612345678  -> +31 (0)61 234 5678

+31612345678   -> +31 (0)61 234 5678 
//add a leading zero between brackets (0))
0031612345678  -> +31 (0)61 234 5678

06 12345678   -> +31 (0)61 234 5678 
//When no country code is given (no 00xx or +xx) than add the dutch one.

This is the function which i have allready:

function formatPhoneNumber($num){
 //delete everything except digits and +
  $number = preg_replace('/[^\d|\+]/', '', $number);

 if(substr($number,0,1) == '+' || substr($number, 0,2) == 00){
  //
 }else{
  $number = "+31".$number;
 }

 /////////
 // formatting as mentioned above
 /////////


 return $num
}

I'm Looking for a algorithm to format the different ways of user inputted telephone numbers to one format: +31 (0)61 2345678

+3  A: 

I'm not sure there's really an international standard for this beyond prefixing the country code. For examples of international phone number formatting, see here. Formatting will change depending on the country, and also on the type.

e.g. in the UK we have +44 (0)20 1234 1234 for landlines, and +44 (0)7700 123456 for mobiles.

Consequently, if you want to do this properly, I think you'll need to identify the country code, and then plug in a country-specific formatter.

Brian Agnew
Thanks. I all ready knew that different countries have different formatting options. Although eventually i want to implement that, for now I'm searching a good way to reformat the user-input string to this format: +31 (0)61 234 5678; A standard accepted here :)
blub
+2  A: 

I've no final solution for this in the moment but would like to point out some difficulties if you don't want to analyze only Dutch numbers:

  • You have to know all international prefixes (some are one-digit like the North-American one +1, some are two-digit like the Dutch one +31, and some are three-digit like the Czech one +420) and if there is no space between country code and area code you cannot simply identify the correct position for the space in your format.

  • Further you cannot assume automatically that a leading zero in the area code is redundant. In some countries like Italy the leading zero has to be used even in international calls, i. e. if you want to call the Milan numer 02 123456 from the Netherlands you have to call +39 02 123456 instead of +39 2 123456.

So, it's not just about writing a short formatting algorithm but very much about understanding the different number formats and usages in different countries.

Of course you can convert Dutch numbers (I assume you exactly know and understand the Dutch phone number format) to the described format and convert every foreign number by just removing all whitespaces of any kind and replace a leading "00" with a "+"--if you want no quick-and-dirty solution you have much work to do.

arno
Ow damn there are more problems to this issue than i in advance thought there would be.
blub
+1  A: 

That's interesting, as it is not the standardized way to format dutch mobile numbers (all start with 06) in the Netherlands.

You should probably just write this test-driven. There are lots of interesting cases you'll want to cover, and that way you'll at least be able to show what you do and what you do not convert.

Stephan Eggermont
That was the first standardization i personnaly added. A prefix/area code always consists of 3 digits: 043 (maastricht), 06x (mobile), 047 (5)roermond.
blub
Dutch users do not like that! A quick business card scan showed only 6 out of 85 people do that.
Stephan Eggermont
I already reverted it; Even I myself couldn't get used to he notation :)
blub