Skip to content

Instantly share code, notes, and snippets.

@ycrao
Last active August 12, 2021 08:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ycrao/930953dfec82813eedce to your computer and use it in GitHub Desktop.
Save ycrao/930953dfec82813eedce to your computer and use it in GitHub Desktop.
中国大陆手机号、车牌号正则验证

大陆手机号

<?php

function validateMobile($mobile, $strict = true) {
  
/*
# 匹配中国大陆国内手机号正则表达式

> 参考资料:http://zh.wikipedia.org/wiki/%E4%B8%AD%E5%8D%8E%E4%BA%BA%E6%B0%91%E5%85%B1%E5%92%8C%E5%9B%BD%E5%A2%83%E5%86%85%E5%9C%B0%E5%8C%BA%E7%A7%BB%E5%8A%A8%E7%BB%88%E7%AB%AF%E9%80%9A%E8%AE%AF%E5%8F%B7%E7%A0%81
> 更新时间:2021-08-12 
> 注:带 <> 表示物联卡和数据卡,带 () 表示卫星电话卡


- 移动: 134[0-8] 135 136 137 138 139 <1440X> <1441X> <147> <148> 150 151 152 157 158 159 <172> 182 183 184 187 188 195 197 198
- 联通: 130 131 132 <145> <146> 155 156 166 171 175 176 185 186 196
- 电信: 133 (1349) <1410X> <149> 153 173 (17400~17405) 177 180 181 189 190 191 193 199
- 广电: 192
- 北京船舶通讯导航有限公司:(1749)
- 虚拟运营商: <10641> 162 165 167 170(0,1,2) 170(3,5,6) 170(4,7,8,9) 171

------------------------------------
排除掉物联、数据和卫星卡,但保留虚拟运营商卡。
        13[0-9]{9}
        14[57]{1}[0-9]{8}
        15[012356789]{1}[0-9]{8}
        16[2567]{1}[0-9]{8}
        17[0123567]{1}[0-9]{8}
        18[0-9]{9}
        19[012356789]{1}[0-9]{8}
------------------------------------
/^13[0-9]{9}|14[57]{1}[0-9]{8}|15[012356789]{1}[0-9]{8}|16[2567]{1}[0-9]{8}|17[0123567]{1}[0-9]{8}|18[0-9]{9}|19[012356789]{1}[0-9]{8}$/
------------------------------------
排除掉物联、数据、卫星和虚拟运营商卡。
        13[0-9]{9}
        14[57]{1}[0-9]{8}
        15[012356789]{1}[0-9]{8}
        166[0-9]{8}
        17[23567]{1}[0-9]{8}
        18[0-9]{9}
        19[012356789]{1}[0-9]{8}
------------------------------------
/^13[0-9]{9}|14[57]{1}[0-9]{8}|15[012356789]{1}[0-9]{8}|166[0-9]{8}|17[23567]{1}[0-9]{8}|18[0-9]{9}|19[012356789]{1}[0-9]{8}$/
------------------------------------
*/
    // 仅排除物联、数据和卫星卡,允许虚拟运营商卡
    $pattern = '/^13[0-9]{9}|14[57]{1}[0-9]{8}|15[012356789]{1}[0-9]{8}|16[2567]{1}[0-9]{8}|17[0123567]{1}[0-9]{8}|18[0-9]{9}|19[012356789]{1}[0-9]{8}$/';
    // 更严格模式,排除物联、数据、卫星和虚拟运营商卡
    $strictPattern = '/^13[0-9]{9}|14[57]{1}[0-9]{8}|15[012356789]{1}[0-9]{8}|166[0-9]{8}|17[23567]{1}[0-9]{8}|18[0-9]{9}|19[012356789]{1}[0-9]{8}$/';
    return preg_match($strict ? $strictPattern : $pattern, $mobile);
}

大陆车牌号

<?php

function validateCarLicense($licenseNo) {
/*
匹配民用车牌和使领馆车牌
判断标准:
-1: 第一位为汉字省份缩写
-2: 第二位为大写字母城市编码
-3: 后面是5位仅含字母和数字的组合
*/
    $regular = "/[京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云渝藏陕甘青宁新使领]{1}[A-Z]{1}[0-9a-zA-Z]{5}$/u";
    preg_match($regular, $licenseNo, $match);
    if (isset($match[0])) {
        return true;
    }
    return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment