Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - RegEx Matching Phone Numbers with or without country code

I want to to write a matching method in php that allows me to match phone numbers.

The case I have is that I want to build an app that will pull phone contact numbers from Android devices, in the backend, it will match the phone contacts with current app users, and if there are matching, then add these users as friends, similar to how whatsapp is doing when matching phone contacts with users who have whatsapp app in their devices.

The issue I am having right now is how I can write a RegEx that can match phone numbers with/without country code or leading zeros.

In the app, I am asking for user country and phone number, so for example a friend of mine entered these values : Country : Jordan (+962) Phone Number : 799633999 This in the backend will be stored as +962799633999

If we assume that I am storying my friends number as 0799633999, what is the regEx that I can use that will match 0799633999 with +96279963999 ?

like image 602
Ahmad Alkhawaja Avatar asked Apr 23 '12 11:04

Ahmad Alkhawaja


2 Answers

Your regex is #(\+\d+|0)(\d{9})$#

like image 63
s.webbandit Avatar answered Sep 27 '22 20:09

s.webbandit


You don't do this with regex. Truncate your leading zero, and country code and do regular string comparation.

If contacts can be from various countries, then add country code of one for whom you are looking friends for.

like image 37
fajter Avatar answered Sep 27 '22 20:09

fajter