站长论坛

标题: PHP正则验证类(PHP5) [打印本页]

作者: superadmin    时间: 2007-10-4 22:34
标题: PHP正则验证类(PHP5)
  1. <?php
  2. /**
  3. *PHP正则验证类
  4. */

  5. class regExp
  6. {
  7.     //去除字符串空格
  8.     static function strTrim($str)
  9.     {
  10.         return preg_replace("/\s/","",$str);
  11.     }

  12.     //验证用户名
  13.     static function userName($str,$type,$len)
  14.     {
  15.         $str=self::strTrim($str);
  16.         if($len<strlen($str))
  17.         {
  18.             return false;
  19.         }else{
  20.             switch($type)
  21.             {
  22.                 case "EN"://纯英文
  23.                     if(preg_match("/^[a-zA-Z]+$/",$str))
  24.                     {
  25.                         return true;
  26.                     }else{
  27.                         return false;
  28.                     }
  29.                     break;
  30.                 case "ENNUM"://英文数字
  31.                     if(preg_match("/^[a-zA-Z0-9]+$/",$str))
  32.                     {
  33.                         return true;
  34.                     }else{
  35.                         return false;
  36.                     }
  37.                     break;
  38.                 case "ALL":    //允许的符号(|-_字母数字)
  39.                     if(preg_match("/^[\|\-\_a-zA-Z0-9]+$/",$str))
  40.                     {
  41.                         return true;
  42.                     }else{
  43.                         return false;
  44.                     }
  45.                     break;
  46.             }
  47.         }
  48.     }

  49.     //验证密码长度
  50.     static function passWord($min,$max,$str)
  51.     {
  52.         $str=self::strTrim($str);
  53.         if(strlen($str)>=$min && strlen($str)<=$max)
  54.         {
  55.             return true;
  56.         }else{
  57.             return false;
  58.         }
  59.     }

  60.     //验证Email
  61.     static function Email($str)
  62.     {
  63.         $str=self::strTrim($str);
  64.         
  65.         if(preg_match("/^([a-z0-9_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.){1,2}[a-z]{2,4}$/i",$str))
  66.         {
  67.             return true;
  68.         }else{
  69.             return false;
  70.         }
  71.         
  72.     }

  73.     //验证身份证(中国)
  74.     static function idCard($str)
  75.     {
  76.         $str=self::strTrim($str);
  77.         if(preg_match("/^([0-9]{15}|[0-9]{17}[0-9a-z])$/i",$str))
  78.         {
  79.             return true;
  80.         }else{
  81.             return false;
  82.         }
  83.     }

  84.     //验证座机电话
  85.     static function Phone($type,$str)
  86.     {
  87.         $str=self::strTrim($str);
  88.         switch($type)
  89.         {
  90.             case "CHN":
  91.                 if(preg_match("/^([0-9]{3}|0[0-9]{3})-[0-9]{7,8}$/",$str))
  92.                 {
  93.                     return true;
  94.                 }else{
  95.                     return false;
  96.                 }
  97.                 break;
  98.             case "INT":
  99.                 if(preg_match("/^[0-9]{4}-([0-9]{3}|0[0-9]{3})-[0-9]{7,8}$/",$str))
  100.                 {
  101.                     return true;
  102.                 }else{
  103.                     return false;
  104.                 }
  105.                 break;
  106.         }
  107.     }
  108. }

  109. $str="008-010-2711204";
  110. if(regExp::Phone("INT",$str))
  111. {
  112.     echo "ok";
  113. }else{
  114.     echo "no";
  115. }

  116. ?>
复制代码





欢迎光临 站长论坛 (https://www.tzlink.com/bbs/) Powered by Discuz! X3.2