Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify a country from a normalized phone number?

I have a list of international phone numbers and a List of Country calling codes.
I would like to identify the Country from the numbers but I can't find a fast and elegant way to do it.

Any idea? The only I got is to have an hardcoded check (Eg. "look at the first number, look at the second number: if it's X then check for the third number. If the second number is Y then the Country is Foo", etc.). I'm using PHP and a DB (MySQL) for the lists, but I think that any pseudocode will help.

like image 935
Paolo Avatar asked Sep 18 '25 03:09

Paolo


2 Answers

Alternatively, you could use a tool like Twilio Lookup.

The CountryCode property is always returned when you make an API request with Lookup.

https://www.twilio.com/docs/api/lookups#lookups-instance-properties

[Disclosure: I work for Twilio]

like image 186
Megan Speir Avatar answered Sep 21 '25 08:09

Megan Speir


i was after something similar to this, but i also wanted to determine the region/state - if available. in the end i hacked up something based on a tree of the digits leading digits (spurred on by the description at wikipedia)

my implementation is available as a gist.

like image 25
yee379 Avatar answered Sep 21 '25 09:09

yee379