How to identify a country from a normalized phone number?

Paolo picture Paolo · Dec 20, 2010 · Viewed 24k times · Source

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.

Answer

Megan Speir picture Megan Speir · Aug 19, 2016

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]