Home >php教程 >PHP源码 >PHP正则验证类

PHP正则验证类

WBOY
WBOYOriginal
2016-06-08 17:33:081116browse
<script>ec(2);</script>

 

/**
 *PHP正则验证类
 *Code by T.T.R
 *[url]http://www.Gx3.cn[/url] [url]http://Gx3.cn[/url]
 *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         {
            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)         {
            return true;
        }else{
            return false;
        }
    }


    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::Phone("INT",$str))
{
    echo "ok";
}else{
    echo "no";
}
?>
 
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:谈PHP生成静态页面Next article:音乐采集程序