Xavi
Xavi

Reputation: 2594

how to get country code from Mobile number in php

Is there any way to find the country code of a phone number in PHP? If I have +971526457896, I will receive the country code as +971 (if its United Arab Emirates).

Any suggestions please, how to do that?

Upvotes: 2

Views: 5197

Answers (1)

Gorka
Gorka

Reputation: 43

It's been a long since you asked this question but I'm going to provide an option to do this for future references.

I had exactly the same situation: all my code+phonenumber are saved on my database in an unique table field...

In order to recognize the international code I use this on my website:

https://github.com/Bluefieldscom/intl-tel-input

Whatever phone number you provide using '+' before your entire phonenumber, it does the correct country code detection.

So, you can use the code database on this code: github.com/Bluefieldscom/intl-tel-input/blob/master/src/js/data.js

The database is the clue of this algorithm, it defines levels of priority for country code coincidences.

And the check de logic in order to perform your own code: github.com/Bluefieldscom/intl-tel-input/blob/master/src/js/intlTelInput.js

U should probably go with the getNumber function.

In any case, using this database, anyone should be able to make a new code in any language.

Good luck!

Upvotes: 1

Related Questions