看到项目中有一个判断手机号运营商的需求,虽然需求已经写完。但也记录一下,防止以后用到。
php判断手机号运营商的方法
php函数
/** * # 判断手机号运营商 * @param int $phone 手机号 * * @return string */ //feiniaomy.com function phone_check($phone){ //移动号码段,网上收集 $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, $phone)){ return '中国移动'; }else if(preg_match($isChinaUnion, $phone)){ return '中国联通'; }else if(preg_match($isChinaTelcom, $phone)){ return '中国电信'; }else{ return '未知运营商'; } }
PS:运营商手机号码的信息收集自网络,可能会有一些不准备。
函数调用:
print_r(phone_check(13512345678)); //中国移动 print_r(phone_check(18612345678)); //中国联通