PHP常用的正则验证(手机号,身份证等校验,身份证生日提取)
所属分类:PHP之旅 | 最后更新于:2020-12-11 09:53:23 | 阅读次数:265

手机号正则验证

PHP

if (!function_exists("isMobile")) { /** * 手机号格式是否正确 * @param $mobile * @return bool */ function isMobile($mobile) { if (preg_match("/^0?(13|14|15|16|17|18|19)[0-9]{9}$/",$mobile)) return true; return false; } }

TP框架

<?php /** * * User: cfn * Date: 2020/12/10 * Email: <cfn@leapy.cn> */ namespace app\validate; use think\Validate; /** * Class UserValidate * @package app\validate */ class UserValidate extends Validate { protected $regex = [ 'tel' => '/^0?(13|14|15|16|17|18|19)[0-9]{9}$/' ]; protected $rule = [ 'mobile' => 'require|length:11|regex:tel', ]; protected $message = [ 'mobile.require' => '手机号不能为空', 'mobile.length' => '手机号必须是11位', 'mobile.regex' => '手机号格式不正确', ]; }

使用

$data['mobile'] = "11896852020"; $uV = new UserValidate(); if (!$uV->check($data)) echo $uV->getError(); echo isMobile($data['mobile']); # 手机号格式不正确 # 不正确

邮箱验证

PHP

if (!function_exists("isEmail")) { /** * 邮箱是否错误 * @param $email * @return bool */ function isEmail($email) { if(preg_match("/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i",$email)) return true; return false; } }

TP框架

<?php /** * * User: cfn * Date: 2020/12/10 * Email: <cfn@leapy.cn> */ namespace app\validate; use think\Validate; /** * Class UserValidate * @package app\validate */ class UserValidate extends Validate { protected $regex = [ 'email' => '/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i' ]; protected $rule = [ 'email' => 'require|regex:email', ]; protected $message = [ 'email.require' => '邮箱不能为空', 'email.regex' => '邮箱格式不正确', ]; }

使用

$data['email'] = "1 cfn@leapy.cn"; $uV = new UserValidate(); if (!$uV->check($data)) echo $uV->getError()."<br/>"; echo (isEmail($data['email']) ? "正确" : "邮箱不正确")."<br/>"; } # 邮箱格式不正确 # 邮箱不正确

银行卡正则验证

PHP

if (!function_exists("isBank")) { /** * 银行卡号是否错误 * @param $bankNum * @return bool */ function isBank($bankNum) { if(preg_match("/^(\d{16}|\d{19}|\d{17})$/i",$bankNum)) return true; return false; } }

TP框架

<?php /** * * User: cfn * Date: 2020/12/10 * Email: <cfn@leapy.cn> */ namespace app\validate; use think\Validate; /** * Class UserValidate * @package app\validate */ class UserValidate extends Validate { protected $regex = [ 'bank' => '/^(\d{16}|\d{19}|\d{17})$/i' ]; protected $rule = [ 'bank' => 'require|regex:bank', ]; protected $message = [ 'bank.require' => '银行卡号不能为空', 'bank.regex' => '银行卡号格式不正确', ]; }

身份证号校验

PHP

if (!function_exists("isIdCard")) { /** * 校验身份证格式是否正确 * 15位身份证没有校验位 * @param $idCard * @return bool */ function isIdCard($idCard) { // 将小写x转换成大写X $idCard = strtoupper($idCard); // 身份证必须是15或18位 $regx = "/(^\d{15}$)|(^\d{17}([0-9]|X)$)/"; $arr = []; if(!preg_match($regx, $idCard)) return false; if(strlen($idCard) === 15) { $regx = "/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/"; preg_match($regx, $idCard, $arr); // 校验日期是否正确 $birthday = "19".$arr[2] . '-' . $arr[3]. '-' .$arr[4]; if(strtotime($birthday)) return true; return false; } else { $regx = "/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/"; preg_match($regx, $idCard, $arr); $dtm_birth = $arr[2] . '-' . $arr[3]. '-' .$arr[4]; if(!strtotime($dtm_birth)) return false; // 加权因子 $weight = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); // 校验码数组 $bits = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); $sign = 0; for ( $i = 0; $i < 17; $i++ ) { $b = (int)$idCard{$i}; $w = $weight[$i]; $sign += $b * $w; } $n = $sign % 11; $val_num = $bits[$n]; if ($val_num == substr($idCard,17, 1)) return true; return false; } } }

身份证上日期提取

if (!function_exists("getBirthdayByIdCard")) { /** * 校验身份证格式是否正确 * 15位身份证没有校验位 * @param $idCard * @return bool */ function getBirthdayByIdCard($idCard) { // 将小写x转换成大写X $idCard = strtoupper($idCard); // 身份证必须是15或18位 $regx = "/(^\d{15}$)|(^\d{17}([0-9]|X)$)/"; $arr = []; if(!preg_match($regx, $idCard)) return false; if(strlen($idCard) === 15) { $regx = "/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/"; preg_match($regx, $idCard, $arr); // 校验日期是否正确 $birthday = "19".$arr[2] . '-' . $arr[3]. '-' .$arr[4]; if(strtotime($birthday)) return $birthday; return false; } else { $regx = "/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/"; preg_match($regx, $idCard, $arr); $dtm_birth = $arr[2] . '-' . $arr[3]. '-' .$arr[4]; if(!strtotime($dtm_birth)) return false; // 加权因子 $weight = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); // 校验码数组 $bits = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); $sign = 0; for ( $i = 0; $i < 17; $i++ ) { $b = (int)$idCard{$i}; $w = $weight[$i]; $sign += $b * $w; } $n = $sign % 11; $val_num = $bits[$n]; if ($val_num == substr($idCard,17, 1)) return $dtm_birth; return false; } } }
文章作者:cfn
本文链接:
500字剩余
昵称:100%2021-01-07 14:14:14
可用
1