简体   繁体   English

PHP - RegEx匹配带或不带国家/地区代码的电话号码

[英]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. 我想在php中编写一个匹配方法,允许我匹配电话号码。

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. 我的情况是,我想构建一个应用程序,从Android设备中提取电话联系号码,在后端,它将匹配与当前应用程序用户的手机联系人,如果有匹配,然后将这些用户添加为朋友,类似于whatsapp在将手机通讯录与在其设备中使用whatsapp app的用户进行匹配时的操作方式。

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. 我现在遇到的问题是我如何编写一个RegEx,可以匹配带/不带国家代码或前导零的电话号码。

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 在应用程序中,我要求用户的国家和电话号码,所以例如我的一个朋友输入了这些值:国家:约旦(+962)电话号码:799633999这在后端将存储为+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 ? 如果我们假设我的朋友编号为0799633999,那么我可以使用的regEx与0799633999匹配+96279963999是什么?

你的正则表达式是#(\\+\\d+|0)(\\d{9})$#

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: php代码:

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

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

print_r($matches);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM