正则表达式验证手机和邮箱格式

if (!function_exists("check_mobile")) {
/**
* 检验手机号格式
* @param $phone 手机号
* @param int $countryCode 区号
* @return bool
* User: 陈启泽
* DateTime: 2020/9/22 14:35
*/
function check_mobile($phone,$countryCode=86)
{
$phone = trim($phone);
if (empty($countryCode)) {
$countryCode = 86;
}
if ($countryCode == 86 ) {
$chars = "/^1[3-9]{1}[0-9]{1}[0-9]{8}$/";
if (preg_match($chars, $phone)) {
return true;
}
}else if ($countryCode == 852) {
if (preg_match('/^[0-9]{8}$/', $phone)) {
return true;
}
}else if ($countryCode == 60) {
if (preg_match('/^0?1(1d{8}|[02-9]d{7})$/', $phone)) {
return true;
}
}else {
if (preg_match('/^[0-9]*$/', $phone)) {
return true;
}
}


return false;
}
}

if (!function_exists("check_email")) {
/**
* 检验邮箱格式
* @param string $email 邮箱
* @return boolean bool true格式正确,false格式错误
* User: 陈启泽
* DateTime: 2020/9/22 14:44
*/
function check_email($email){
$email = trim($email);
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
return true;
}else{
return false;
}
}
}
原文地址:https://www.cnblogs.com/qize/p/13897773.html