Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split mobile number into country code, area code and local number?

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

like image 984
Vivart Avatar asked Mar 30 '10 08:03

Vivart


People also ask

How do you write your mobile number with country code and area code?

For example, if a contact in the United States (country code "1") has the area code "408" and phone number "XXX-XXXX", you'd enter +1 408 XXX XXXX.

How do you enter mobile number prefix and country code?

Mobile numbers which are not local need to be prefixed by a 0 while dialing, or by +91 (91 is the country code for India). A mobile number written as +91-AAAAA BBBBB is valid throughout India, and in other countries where the + is recognized as a prefix to the country code.

How phone number is divided?

Think of the three parts like a street address, where the area code is the city, the prefix is the street and the line number is the house. Of course, when you're calling another country, you must first dial 011, which is the international access code, and then the country code.


7 Answers

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!

like image 107
Kevin Bourrillion Avatar answered Oct 05 '22 08:10

Kevin Bourrillion


It's not possible to parse phone numbers with a simple algorithm, you need to use data tables populated with each country's rules - because each country delimits their phone numbers differently.

The country code is fairly easy, just use the data from from the Country calling codes article in wikipedia and build a table of all the unique country codes. Each country has a unique prefix, so that's easy.

But then you need to look up the rules for every country you want to support and extract the area code(s) using the rules for each country.

like image 40
Dean Harding Avatar answered Oct 05 '22 07:10

Dean Harding


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 :-

+------------+---------+-------+--------------+
|country_code|area_code|country|area          |
+------------+---------+-------+--------------+
|44          |1634     |UK     |Medway        |
|44          |20       |UK     |London        |
|964         |23       |Iraq   |Wasit (Al Kut)|
|964         |2412     |Iraq   |Unreal        |
+------------+---------+-------+--------------+

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.

 DECLARE @area_codes TABLE
(
    country_code VARCHAR(10),
    area_code VARCHAR(10),
    country VARCHAR(20),
    area VARCHAR(20),
    match_string VARCHAR(MAX),
    match_length INTEGER
)

INSERT INTO @area_codes VALUES ('44','1382','UK','Dundee', '441382%', 6)
INSERT INTO @area_codes VALUES ('44','1386','UK','Evesham', '441386%', 6)
INSERT INTO @area_codes VALUES ('44', '1', 'UK', 'Geographic numbers', '441%', 3)

DECLARE @number VARCHAR(MAX)
SET @number = '441386111111'

SELECT TOP 1 * 
FROM @area_codes 
WHERE @number LIKE match_string
ORDER BY match_length DESC

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.

like image 30
Steve Weet Avatar answered Oct 05 '22 08:10

Steve Weet


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..

like image 31
chriszero Avatar answered Oct 05 '22 08:10

chriszero


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"):

  • +420123456789 is a (bogus) number in Czech Republic (country code +420 ), and the rest IS the local number (some countries use an undivided addressing space, although you could infer a few bits of data from the first 1-4 digits of the local number (e.g. "+420800 are toll-free numbers")). So, the only useful way to parse this number is into two parts, +420 123456789.
  • +18005551234 is a (probably also bogus) number in the US; according to the North American numbering plan, +1 is country code, 800 is area code (toll-free numbers), 555 is exchange code and 1234 is local number. You can then parse the number into four parts, +1 800 555 1234.
like image 32
Piskvor left the building Avatar answered Oct 05 '22 07:10

Piskvor left the building


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.

like image 2
g1smd Avatar answered Oct 05 '22 07:10

g1smd


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

  • 49 is the country code of Germany
  • 30 is the area code of Berlin
  • 123456789 is a local number in Berlin (no real one)

Example: 493328123456

  • 49 is the country code of Germany
  • 3328 is the area code of Teltow
  • 123456 is a local number in Teltow (no real one)

Example: 34971123456

  • 34 is the country code of Spain
  • 971 is the area code of Mallorca
  • 123456 is a local number on Mallorca (no real one)
like image 1
Arne Burmeister Avatar answered Oct 05 '22 09:10

Arne Burmeister