首页 > PHP教程 > php开发知识文章

PHP 常用用户注册信息的正则验证helper公共类

本文主要介绍了PHP 常用用户注册信息的正则验证helper公共类。分享给大家供大家参考,欢迎大家的学习。

常规验证,手机号、电话、小灵通验证,字符串长度区间合法验证,邮箱验证,使用正则验证数据。

/**
* 常用信息验证helper公共类
*/
class CheckInfo
{
//手机号/电话/小灵通 验证
public function phone_check($mobile, $type = array())
{
/**
* 手机号码
* 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
* 联通:130,131,132,152,155,156,185,186
* 电信:133,1349,153,180,189
*/
$res[1] = preg_match('/^1(3[0-9]|5[0-35-9]|8[0-9])\\d{8}$/', $mobile);
/**
* 中国移动:China Mobile
* 11 * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
*/
$res[2] = preg_match('/^1(34[0-8]|(3[5-9]|5[017-9]|8[0-9])\\d)\\d{7}$/', $mobile);
/**
* 中国联通:China Unicom
* 130,131,132,152,155,156,185,186
*/
$res[3] = preg_match('/^1(3[0-2]|5[256]|8[56])\\d{8}$/', $mobile);
/**
* 中国电信:China Telecom
* 133,1349,153,180,189
*/
$res[4] = preg_match('/^1((33|53|8[09])[0-9]|349)\\d{7}$/', $mobile);
/**
* 大陆地区固话及小灵通
* 区号:010,020,021,022,023,024,025,027,028,029
* 号码:七位或八位
*/
$res[5] = preg_match('/^0(10|2[0-5789]|\\d{3})-\\d{7,8}$/', $mobile);
$type = empty($type) ? array(1, 2, 3, 4, 5) : $type;
$ok = false;
foreach ($type as $key => $val) {
if ($res[$val]) {
$ok = true;
}
continue;
}
if ($mobile && $ok) {
return true;
} else {
return false;
}
}

//字符串长度区间合法验证
public function strlen_check($str, $min = NULL, $max = NULL)
{
preg_match_all("/./u", $str, $matches);
$len = count($matches[0]);
if (is_null($min) && !empty($max) && $len < $max) {
return false;
}
if (is_null($max) && !empty($min) && $len > $min) {
return false;
}
if ($len < $min || $len > $max) {
return false;
}
return true;
}

//邮箱验证
public function isEmail($str)
{
if (!$str) {
return false;
}
return preg_match('#[a-z0-9&\-_.]+@[\w\-_]+([\w\-.]+)?\.[\w\-]+#is', $str) ? true : false;
}

/**
* 使用正则验证数据
* @access public
* @param string $rule 验证规则
* @param string $value 要验证的数据
* @return boolean
*/
public function regex($rule, $value)
{
$validate = array(
//字段必须,不能为空
'require' => '/\S+/',
//邮箱验证
'email' => '/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/',
//url验证
'url' => '/^http(s?):\/\/(?:[A-za-z0-9-]+\.)+[A-za-z]{2,4}(?:[\/\?#][\/=\?%\-&~`@[\]\':+!\.#\w]*)?$/',
//货币验证
'currency' => '/^\d+(\.\d{0,2})?$/',
//数字验证
'number' => '/^[-\+]?\d+(\.\d+)?$/',
//zip验证
'zip' => '/^\d{6}$/',
//整数验证
'integer' => '/^[-\+]?\d+$/',
//浮点数验证
'double' => '/^[-\+]?\d+(\.\d+)?$/',
//英文验证
'english' => '/^[A-Za-z]+$/',
'gt0' => '/^(?!(0[0-9]{0,}$))[0-9]{1,}[.]{0,}[0-9]{0,}$/',
//合法帐号
'account' => '/^[a-zA-Z][a-zA-Z0-9_]{1,19}$/'
);
// 检查是否有内置的正则表达式
if (isset($validate[strtolower($rule)]))
$rule = $validate[strtolower($rule)];
return preg_match($rule, $value) === 1;
}

public function check_pwd($pwd, $min = NULL, $max = NULL)
{
if (strlen($pwd) > $max || strlen($pwd) < $min || preg_match("/^\d*$/", $pwd) || preg_match("/^[a-z]*$/i", $pwd)) {
return false;
}
return true;
}
}

is_null() 检测变量是否为 NULL。

JavaScript正则表达式在线测试工具:
http://tools.jb51.net/regex/javascript

正则表达式在线生成工具:
http://tools.jb51.net/regex/create_reg

以上就是本文PHP 常用用户注册信息的正则验证helper公共类的全部内容,希望本文所述对大家PHP程序设计有所帮助。

关闭
感谢您的支持,我会继续努力!
扫码打赏,建议金额1-10元


提醒:打赏金额将直接进入对方账号,无法退款,请您谨慎操作。