zl程序教程

您现在的位置是:首页 >  其他

当前栏目

PHP 获取手机号运营商

2023-02-18 16:33:57 时间

1. 前言


公司有一个话费充值项目,需要获取手机号的运营商,进行执行不同的逻辑。

根据手机号的前三位就可以知道手机号的运营商

移动:134、135、136、137、138、139、150、151、157(TD)、158、159、187、188

联通:130、131、132、152、155、156、185、186

电信:133、153、180、189、(1349卫通)

2. PHP 获取手机号的运营商


这是我通过查找资料得到的一个函数,测试确实可用

/**
* 获取手机号运营商
*
* @param $mobile
* @return mobile|union|telcom|unknown 移动|联通|电信|未知
*/
function getMobileServiceProvider($mobile)
{
$isChinaMobile = "/^134[0-8]\d{7}$|^(?:13[5-9]|147|15[0-27-9]|178|18[2-478])\d{8}$/";
$isChinaUnion = "/^(?:13[0-2]|145|15[56]|176|18[56])\d{8}$/";
$isChinaTelcom = "/^(?:133|153|177|173|18[019])\d{8}$/";
if (preg_match($isChinaMobile, $mobile)) {
return 'mobile'; // 移动
} else if (preg_match($isChinaUnion, $mobile)) {
return 'union'; // 联通
} else if (preg_match($isChinaTelcom, $mobile)) {
return 'telcom'; // 电信
} else {
return 'unknown'; // 未知
}
}