车牌号匹配
#匹配民用车牌和使馆车牌
# 判断标准
# 1,第一位为汉字省份缩写
# 2,第二位为大写字母城市编码
# 3,后面是5位仅含字母和数字的组合
$regular = "/[京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云渝藏陕甘青宁新使]{1}[A-Z]{1}[0-9a-zA-Z]{5}$/u"; preg_match($regular, $license, $match); if (isset($match[0])) { return true; }
#匹配特种车牌(挂,警,学,领,港,澳)
$regular = '/[京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云渝藏陕甘青宁新]{1}[A-Z]{1}[0-9a-zA-Z]{4}[挂警学领港澳]{1}$/u'; preg_match($regular, $license, $match); if(isset($match[0])){ true; }
#匹配武警车牌
$regular = '/^WJ[京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云渝藏陕甘青宁新]?[0-9a-zA-Z]{5}$/ui'; preg_match($regular, $license, $match); if (isset($match[0])) { return true; }
#匹配军牌
$regular = "/[A-Z]{2}[0-9]{5}$/"; preg_match($regular, $license, $match); if (isset($match[0])) { return true; }
#匹配新能源车辆6位车牌
#小型新能源车 $regular = "/[京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云渝藏陕甘青宁新]{1}[A-Z]{1}[DF]{1}[0-9a-zA-Z]{5}$/u"; preg_match($regular, $license, $match); if (isset($match[0])) { return true; } #大型新能源车 $regular = "/[京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云渝藏陕甘青宁新]{1}[A-Z]{1}[0-9a-zA-Z]{5}[DF]{1}$/u"; preg_match($regular, $license, $match); if (isset($match[0])) { return true; }
手机号匹配
$phonenumber = '13712345678'; if(preg_match("/^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$/",$phonenumber)){ echo "是手机号码"; }else{ echo "不是手机号码"; }
注:"^"匹配文本的开头,"$"匹配文本的结尾。
验证身份证号码是否正确函数
$IDCard='身份证'; $preg = '/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/i'; if(preg_match($preg,$IDCard)){ echo'正确'; }else{ echo'错误!'; }
验证码邮箱
$mail = '123456@qq.com'; //邮箱地址 $pattern = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/"; $res = preg_match($pattern, $mail, $matches); if($res){ echo'正确'; }else{ echo'错误!'; }
正则匹配单双引号之间内容案例
$a = 'lsadlsadals<"dasdas"> dsakdnas'; $s = preg_match("/<[\"|\'](.*?)[\"|\']>/",$a,$m); if($s){ var_dump($m); }else{ echo '无匹配!'; }