Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching users with formatted and unformatted phone numbers (from contacts, internationally)

This is what we have :

Users in a database, including a phone_number field. When users create their account, they have to enter their phone number. We can ask for a specific formatting, and we do :

+ [countrycode] [phone number]

The user chooses the country code and adds the required numbers. We assume he enters it correctly of course.

Now he is in the database, and we're trying to match OTHER users with the numbers we found in his contacts. This is where we have a problem :

He can save his contacts with different number formatting (national, international, with+, without +). I'll take belgian number as an example to make my point clearer :

+32 495 12 34 56 (international)
0032 495 12 34 56 (international)
0 495 12 34 56 (national)

I'm guessing most users will save their contacts with the national formatting, and this is the problem.

Our app needs have the international formatting, and we need to compare the users from the DB with the contacts (this is a part of our "spread the app" plan) to let the user chose wether he can invite a contact or not. and we don't want to show people who don't have the app (among other things).

How can I know if the phone number is formatted internationnaly or nationally, and either way, how can I transform it to be international?

If there is a + i can assume its the right format, so there i have no issue. If there isn't, what can I do? I've thought of this but i'm not even sure :

  • Any number that starts with 00 is 100% international? And i just change 00 into + and i'm good?
  • Any number that doesn't fit the two other critiera, I ... ? I don't even know.

Maybe get every number format in the world and use an incredibly long switch statement to transform it with the right international format? I doubt this is the right answer.

Anyway, any clue/advise/solution is obviously most welcome !

Note : I'm using Parse.com for storing data, if this is of any help.

like image 317
Gil Sand Avatar asked Oct 31 '22 15:10

Gil Sand


1 Answers

Maybe libphonenumber is what you are searching for: https://code.google.com/p/libphonenumber/

like image 95
eztam Avatar answered Nov 15 '22 08:11

eztam