PHP正则验证类(PHP5)

字体大小: 中小 标准 ->行高大小: 标准
没事自己写着玩的,有兴趣的朋友继续扩展啊,别忘了发我一份哦,哈哈谢谢~

guyudj@yahoo.com.cn

[PHP]
<?php
/**
*PHP正则验证类
*Code by T.T.R
*http://www.Gx3.cn http://Gx3.cn
*QQ:252319874
*/

class regExp
{
//去除字符串空格
static function strTrim($str)
{
return preg_replace("/\s/","",$str);
}

//验证用户名
static function userName($str,$type,$len)
{
$str=self::strTrim($str);
if($len<strlen($str))
{
return false;
}else{
switch($type)
{
case "EN"://纯英文
if(preg_match("/^[a-zA-Z]+$/",$str))
{
return true;
}else{
return false;
}
break;
case "ENNUM"://英文数字
if(preg_match("/^[a-zA-Z0-9]+$/",$str))
{
return true;
}else{
return false;
}
break;
case "ALL": //允许的符号(|-_字母数字)
if(preg_match("/^[\|\-\_a-zA-Z0-9]+$/",$str))
{
return true;
}else{
return false;
}
break;
}
}
}

//验证密码长度
static function passWord($min,$max,$str)
{
$str=self::strTrim($str);
if(strlen($str)>=$min && strlen($str)<=$max)
{
return true;
}else{
return false;
}
}

//验证Email
static function Email($str)
{
$str=self::strTrim($str);

if(preg_match("/^([a-z0-9_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.){1,2}[a-z]{2,4}$/i",$str))
{
return true;
}else{
return false;
}

}

//验证身份证(中国)
static function idCard($str)
{
$str=self::strTrim($str);
if(preg_match("/^([0-9]{15}|[0-9]{17}[0-9a-z])$/i",$str))
{
return true;
}else{
return false;
}
}

//验证座机电话
static function Phone($type,$str)
{
$str=self::strTrim($str);
switch($type)
{
case "CHN":
if(preg_match("/^([0-9]{3}|0[0-9]{3})-[0-9]{7,8}$/",$str))
{
return true;
}else{
return false;
}
break;
case "INT":
if(preg_match("/^[0-9]{4}-([0-9]{3}|0[0-9]{3})-[0-9]{7,8}$/",$str))
{
return true;
}else{
return false;
}
break;
}
}
}

$str="008-010-2711204";
if(regExp:hone("INT",$str))
{
echo "ok";
}else{
echo "no";
}

?>
[/PHP]

此文章由 http://www.ositren.com 收集整理 ,地址为: http://www.ositren.com/htmls/29994.html