5

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 ?

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

But country codes is not always a 3 digits, I was trying to find a generic regex that take into account any country codes
2

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.

1 Comment

I expect that REGEX will truncate that for me and will make the matching by excluding the leading zero and country code. It is just that I cannot guess if the number in the phone contacts comes with leading zero/country code or not, so regex will make sure to exclude that from both strings and do the matching, no?
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.