drag-create-api/app/common/exception/RegularVerification.php
2023-06-25 08:51:24 +08:00

50 lines
2.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\common\exception;
/**
* 正则验证类
*/
class RegularVerification extends \Exception
{
/**
* 正则匹配验证字符串
* @params string $str 需要匹配验证的字符串
* @params string $type 匹配类型 手机号:mobile(默认),邮箱:email,网址:url,身份证号:idcard,邮编:post,ip地址:ip
*/
public static function checkPreg($str, $type = "")
{
if ($str) {
if ($type == '' || $type == 'mobile') {
//验证手机号 默认
return preg_match('#^1[3,8]{1}\d{9}$|^14[5-9]{1}\d{8}$|/^15[\d^4]{1}\d{8}$/|^16[5,6]{1}\d{8}$|^17[0-8]{1}\d{8}$|^19[0,2,6,7,8,9]{1}\d{8}$#', $str) ? true : false;
} else if ($type == 'email') {
//验证邮箱
return preg_match('#[a-z0-9&\-_.]+@[\w\-_]+([\w\-.]+)?\.[\w\-]+#is', $str) ? true : false;
} else if ($type == 'url') {
//验证url
return preg_match('#(http|https|ftp|ftps)://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?#i', $str) ? true : false;
} else if ($type == 'idcard') {
//验证身份证号
return preg_match('#(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)#', $str) ? true : false;
} else if ($type == 'post') {
//验证邮编
return preg_match('#^[0-9][\d]{5}$#', $str) ? true : false;
} else if ($type == 'ip') {
//验证ip地址
if (preg_match('#^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$#', $str)) {
$ipArray = explode('.', $str);
//真实的ip地址每个数字不能大于2550-255
return $ipArray[0] <= 255 && $ipArray[1] <= 255 && $ipArray[2] <= 255 && $ipArray[3] <= 255 ? true : false;
} else {
return false;
}
} else {
return false;
}
} else {
return false;
}
}
}