Solved

Parsing International Phone Numbers

Posted on 2007-12-02
10
2,598 Views
Last Modified: 2013-12-12
I am trying to write a routine to parse international phone numbers, more specifically I want to extract the country code. I have a table of all the country codes, however, with variable numbering systems in Europe, I'm thinking it may be tough to do so reliably.

In calculating the country codes I hope they have arranged them so
that there is no way to overlap. For instance, if one country has
12345  (A) and another has 123 (B), then if I search for 123, I'll get
two hits, but only one is right. I can get around this by checking the
longest ones first

BUT

What happens if country B has a phone number like

123454545454545 where 123 is the country code and 454545454545 is the number

If I don't do it right, I could also accidentally get a hit on country A which has a country code of 12345.

I'm currently writing this routine in PHP. I'm not sure what section this would really go under.
0
Comment
Question by:dignified
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 200 total points
ID: 20393865
>If I don't do it right, I could also accidentally get a hit on country A which has a country code of 12345.
if you really had a numbering plan code with 12345, then 123 is a more general code of the same country, and 12345 is only a specific part, like only the mobile numbers range.

so, from a given number, the code to look for is the longest match.

say you have 1 table with all the prefix codes:
code   country
352    Luxemburg (range for all luxemburg phones, fixed + cellular)
3526   Luxemburg - Mobile (range for all luxemburg cellular phones)
et ....

assuming you have mysql, the query would be








select t.*
from yourtable t
where '35269140923040' LIKE code || '%'
order by length(t.code)
limit 1;

Open in new window

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20393867
the code snippet above would return 3526   Luxemburg mobile, btw...
0
 

Author Comment

by:dignified
ID: 20393895
It won't necessarily be the longest one though will it? Perhaps the system is worked out so that this is true? But with any random number this isn't the case.
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 25

Expert Comment

by:kode99
ID: 20393901
The wikipedia entries for the phone numbering system contains all the information you need.  You will have to do a few checks to make sure any given number fully fits a given format before you pull out the country code - this way you can be sure of getting it right.

Dial plan info,
http://en.wikipedia.org/wiki/Telephone_numbering_plan

Coutry code info
http://en.wikipedia.org/wiki/Country_calling_codes
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20393943
>Perhaps the system is worked out so that this is true?
yes, the system is worked out that way, otherwise it could not work.

>But with any random number this isn't the case.
have you any proof of the contrary?
note: I work as billing engineer in a telco company, so I know what I am speaking about :)
0
 

Author Comment

by:dignified
ID: 20393962
>yes, the system is worked out that way, otherwise it could not work.
This is probably all I need to know.


>have you any proof of the contrary?
Didn't you just agree with me in your previous comment? Read my original question for an example of what I'm talking about.  As long as all I have to do is use the longest match, then that is fine with me.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20394156
>As long as all I have to do is use the longest match, then that is fine with me.
you have to.

>>have you any proof of the contrary?
I am sure you could not have such a proof, but maybe you "found" an example where you had trouble identifying the correct code...


note: the example I showed for luxemburg 352 and 3526 can be differentiated in your tables, where you could say 352 = country code 3526 = country mobile subnet
then, depending on what you are looking for, you could exclude the subnets for example in your search, so you find the actual country and not the "deepest match" subnet...
0
 
LVL 25

Assisted Solution

by:kode99
kode99 earned 50 total points
ID: 20400004
It really does not matter if you do longest or shortest,  either will be right,  but if you work from the left to the right your search will be faster and more efficient as it will find the short codes faster.  Not surprisingly - all the larger countrys have 2 digit codes,  which statistically will likely be were most of the numbers you lookup will be.

There are no overlaps between short codes and longer codes.  For example Egypt is 20,  there is no other country code whole first two digital are 20. So once you find 20 as the first two digits you know you are done and have the country code.  If you are looking at a number from Croatia,  code 385, with the first two digits of 38,  at this point you know there is no country with just 38 so you have to look at one more digit to get the 5 for the full code.

If you look at the chart of country codes I linked above it is laid out so you can easily see this.  The whole system has its roots in how the original mechanical switching system operated.  Also why the codes are more or less organized by regions on the globe.

Of course North America is the exception to the numbering system though with the leading '1' it is still not going to conflict anyway.

To really verify things you can then format check the rest of the number to be sure it has the right number of digits for the numbering used in the country specified.  This can also be a sanity check which can be useful if the numbers have been entered manually and are likely to have mistakes.
0
 

Author Comment

by:dignified
ID: 20400256
>To really verify things you can then format check the rest of the number to be sure it has the right number >of digits for the numbering used in the country specified.  This can also be a sanity check which can be >useful if the numbers have been entered manually and are likely to have mistakes.

Yes, the right number of digits is what I was also worried about since I don't have a table with this information.
0
 
LVL 25

Expert Comment

by:kode99
ID: 20415225
Unfortunatly I don't know of a nice table of this information but you can get the full details for any given country's internal numbering system through the ITU website here,

http://www.itu.int/oth/T0202.aspx?parent=T0202



Here's an example of exchange information for Africa,

http://www.wtng.info/ccod-2.html#CC255

This is from the World Telephone Numbering guide,
http://www.wtng.info/





0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Moving your enterprise fax infrastructure from in-house fax machines and servers to the cloud makes sense — from both an efficiency and productivity standpoint. But does migrating to a cloud fax solution mean you will no longer be able to send or re…
What do we know about Legacy Video Conferencing? - Full IT support needed! - Complicated systems at outrageous prices! - Intense training required! Highfive believes we need to embrace a new alternative.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question