phpandroidregexcordovamobile-country-code

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 ?


Solution

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