Question

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 ?

Was it helpful?

Solution

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

OTHER TIPS

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.

refer to this link you can use this regex:

^([0|\+[0-9]{1,5})?([7-9][0-9]{9})$

php code:

$re = '/^([0|\+[0-9]{1,5})?([7-9][0-9]{9})$/';
$str = '+2529371208646';

preg_match_all($re, $str, $matches);

print_r($matches);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top