常用验证类
一、验证手机号是否合法
请求参数类型:int
返回值类型:bool
public function isLegalPhone($phone)
{
if(!preg_match("/^1[345678]{1}\d{9}$/",$phone))
{
return false;
}
return true;
}
$obj = new ValidateHandle();
$res = $obj->isLegalPhone(15888854112);
vd($res);
输出:
bool(true)
二、验证邮箱是否合法
请求参数类型:string
返回值类型:bool
public function isLegalMail($email)
{
$para = "/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*\.)+[a-zA-Z]*)$/u";
if(preg_match($para, $email)){
return true;
}else{
return false;
}
}
$obj = new ValidateHandle();
$res = $obj->isLegalMail('1843622555@qq.com');
vd($res);
输出:bool(true)
三、验证 URL 是否合法
请求参数类型:string
返回值类型:bool
public function isLegalUrl($url)
{
$r = "/http[s]?:\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*/is";
if (preg_match($r, $url)) {
return true;
} else {
return false;
}
}
$obj = new ValidateHandle();
$res = $obj->isLegalUrl('https://www.nntool.cc/');
vd($res);
输出:bool(true)
四、验证 IP 是否合法
请求参数类型:string
返回值类型:bool
public function isLegalIp($ip) {
if (preg_match('/^((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1 -9]?\d))))$/', $ip)) {
return true;
} else {
return false;
}
}
$obj = new ValidateHandle();
$res = $obj->isLegalIp('210.22.245.36');
vd($res);
输出:bool(true)