How to split mobile number into country code, area code and local number? e.g +919567123456 after split
country code = 91
area code = 9567
local number = 123456
How to split mobile number into country code, area code and local number? e.g +919567123456 after split
country code = 91
area code = 9567
local number = 123456
If you strive accurate UK data see also http://code.google.com/p/ofcom-csverter/ for a complete list of UK area codes with corrections.
I think you will need something like a dictonary of country and area codes. because booth of them can have a different lenght. USA +1, Germany +49, even +6723. Same with the Areacodes..
A very complex problem. First you need to determine the country code. Depending on the country code, the rest has to be splitted into area code and local number. But none of the three parts has a fixed length, not the hole number nor the area code and local part combination!
Example: 4930123456789
Example: 493328123456
Example: 34971123456
Don't maintain your own table of all this data! Use the "Java International Phone Number Utilities library v3.0", https://github.com/googlei18n/libphonenumber. This is what Google uses, and Google maintains it for you!
As mentioned by various people you can not do this with simple string matching. The lengths of neither country nor area codes are fixed.
Having done this in the past we maintained a table similar in structure to the following :-
We then calculated the maximum length of area_code and country_code and checked the string by sub-stringing starting at the maximum length and working our way down until we found a match.
So given the number 441634666788
We would have started at the substring[1,7] (7 being the length of the longest country/area code combination), not found a match, then moved on to [1,6] and found the match for UK/Medway.
Not very efficient but it worked.
EDIT
You could also try something like this but you would need to test it with a full data set or maybe even break it down into separate country and area code selects as it may not be very performant with your chosen DB.
You would maintain the match_string and match_length fields through a trigger, taking care to cope with null area codes and index the table on the match_string column.
The answer very much depends on the country. There is no universal rule saying "this is country code, this is area code, this is local number". The only information that can be gained universally is the country number (and even that can be 1-4 digits long); then you need to consult the specific country's ruleset.
For examples (like, "there are many different phone numbers in the given countries, but they all follow the same format"):