PHP中比较值得推荐的数据验证的类

  1. <?php
  2. /*
  3. 我的数据验证的类
  4. */
  5. class checker{
  6. // 函数定义
  7. var $array_data="";     //要验证的数组数据
  8. var $var_key="";     //当前要验证的数据的key
  9. var $var_value="";     //当前要验证的数据的值
  10. var $is_empty="";     //要验证的值可以为空
  11. var $array_info="";     //提示信息收集
  12. var $array_errors=array();   //错误信息收集
  13. //--------------------->构造函数<------------
  14. function checker($date){
  15. $this->array_data=$date;
  16. }
  17. //--------------------->数据检验函数<-------------
  18. function check($array_datas){
  19. foreach($array_datas as $value_key => $value_v){
  20.  $temp1=explode('|',$value_v);
  21.  if($temp1[0]=="i_empty" and empty($this->array_data[$value_key])){
  22.  ;
  23.  }else{
  24.  foreach($temp1 as $temp_key => $value_con){
  25.   //$data_temp=$this->array_data;
  26.   //var_dump($data_temp['birthday']);
  27.   //echo "--".$value_key."--<br>";
  28.   $this->var_key=$value_key;
  29.   $this->var_value=$this->array_data[$value_key];
  30.   $temp2=explode(':',$value_con);
  31.   switch(count($temp2)){
  32.    case 0:
  33.     $this->array_errors[$this->var_key]="此值的验证请求不存在";
  34.     break;
  35.    case 1:
  36.     //如果用户没有指定验证动作
  37.     if(empty($temp2[0])){
  38.      $this->array_errors[$this->var_key]="此值的验证请求不存在";
  39.      break;
  40.     }else{
  41.      $this->$temp2[0]();   //如果返回值为非,就不用进行下一步验证
  42.      break;
  43.     }
  44.    case 2:
  45.     $this->$temp2[0]($temp2[1]);
  46.     break;
  47.    case 3:
  48.     $this->$temp2[0]($temp2[1],$temp2[2]);
  49.     break;
  50.   }
  51.  }
  52.  }
  53. }
  54. }
  55. function i_empty(){
  56. $this->is_empty=1;  //这个值没什么用,只是说明要验证的值可以是空值
  57. }
  58. //日期数据、邮件地址、浮点数据、整数、IP地址、字符串、最大值、最小值、字符串长度、域名、URL
  59. //-------------------->日期验证--------------------
  60. function i_date(){
  61.   //约定格式:2000-02-01或者:2000-5-4
  62.      if (!eregi("^[1-9][0-9][0-9][0-9]-[0-9]+-[0-9]+$", $this->var_value)) {
  63.    $this->array_errors[$this->var_key]="日期格式错误";
  64.         return false;
  65.      }
  66.      $time = strtotime($this->var_value);
  67.      if ($time === -1) {
  68.    $this->array_errors[$this->var_key]="日期格式错误";
  69.         return false;
  70.      }
  71.      $time_e = explode('-', $this->var_value);
  72.      $time_ex = explode('-', Date('Y-m-d', $time));
  73.      for ($i = 0; $i < count($time_e); $i++) {
  74.         if ((int)$time_e[$i] != (int)$time_ex[$i]) {
  75.    $this->array_errors[$this->var_key]="日期格式错误";
  76.            return false;
  77.         }
  78.      }
  79.      return true;
  80. }
  81. //-------------------->时间验证--------------------
  82. function i_time() {
  83. if (!eregi('^[0-2][0-3](:[0-5][0-9]){2}$', $this->var_value)) {
  84.  $this->array_errors[$this->var_key]="时间格式错误";
  85.        return false;
  86. }
  87. return true;
  88. }
  89. //-------------------->email验证--------------------
  90. function i_email(){
  91. if(!eregi("^[0-9a-z~'!#$%&_-]([.]?[0-9a-z~!#$%&_-])*" .
  92.  "@[0-9a-z~!#$%&_-]([.]?[0-9a-z~!#$%&_-])*$", $this->var_value))
  93.  $this->array_errors[$this->var_key]="邮件格式错误<br>";
  94.  //echo $this->var_value;
  95. return true;
  96. }
  97. //-------------------->浮点数验证--------------------
  98. function i_float(){
  99. //if(!is_float($this->var_value))
  100. if(!ereg("^[1-9][0-9]?/.([0-9])+$",$this->var_value))
  101.  $this->array_errors[$this->var_key]="这不是一个小数";
  102. }
  103. //-------------------->字符串验证--------------------
  104. function i_string(){
  105. if(empty($this->var_value))    //允许为空
  106.  return true;
  107. if(!is_string($this->var_value))
  108.  $this->array_errors[$this->var_key]="这不是一个字符串";
  109. return true;
  110. }
  111. //-------------------->字符串长度验证--------------------
  112. function len($minv,$maxv=-1){
  113.      $len = strlen($this->var_value);
  114.   if($len==0){
  115.    $this->array_errors[$this->var_key]="不能为空值";
  116.    return false;
  117.   }
  118.      if ($len < $minv) {
  119.    $this->array_errors[$this->var_key]="输入的串太短了";
  120.         return false;
  121.      }
  122.      if ($maxv != -1) {
  123.         if ($len > $maxv) {
  124.    $this->array_errors[$this->var_key]="输入的串太长了";
  125.            return false;
  126.         }
  127.      }
  128.      return true;
  129. }
  130. //-------------------->整数验证--------------------
  131. function i_int(){
  132. if(!ereg("^[0-9]*$",$this->var_value))
  133.  $this->array_errors[$this->var_key]="这不是一个整数";
  134. }
  135. //-------------------->IP地址验证--------------------
  136. function i_ip(){
  137. if(!ereg("^[0-9]{1,3}/.[0-9]{1,3}/.[0-9]{1,3}/.[0-9]{1,3}$", $this->var_value)){
  138.  $this->array_errors[$this->var_key]="错误的IP地址";
  139. }else{
  140.  //每个不大于255
  141.  $array_temp=preg_split("//./",$this->var_value);
  142.  foreach($array_temp as $ip_value){
  143.   if((int)$ip_value >255)
  144.    $this->array_errors[$this->var_key]="错误的IP地址";
  145.  }
  146. }
  147. return true;
  148. }
  149. //-------------------->最大值验证--------------------
  150. function i_max($maxv){
  151. if($this->var_value >= $maxv){
  152.  $this->array_errors[$this->var_key]="数据值太大";
  153.  return false;
  154. }
  155. return true;
  156. }
  157. //-------------------->最小值验证--------------------
  158. function i_min($minv){
  159. if($this->var_value <= $minv){
  160.  $this->array_errors[$this->var_key]="数据值太小";
  161.  return false;
  162. }
  163. return true;
  164. }
  165. //-------------------->域名验证--------------------
  166. function i_domain() {
  167. if(!eregi("^@([0-9a-z/-_]+/.)+[0-9a-z/-_]+$", $this->var_value))
  168.  $this->array_errors[$this->var_key]="错误的域名";
  169. return eregi("^@([0-9a-z/-_]+/.)+[0-9a-z/-_]+$", $this->var_value);
  170. }
  171. //-------------------->URL验证--------------------
  172. function i_url(){
  173. if(!eregi('^(http://|https://){1}[a-z0-9]+(/.[a-z0-9]+)+$' , $this->var_value))
  174.  $this->array_errors[$this->var_key]="错误的WEB地址";
  175. return true;
  176. }
  177. //-------------------->自定义正则校验--------------------
  178. function check_own($user_pattern){
  179. //自定义校验。出错返回false,匹配返回1,不匹配返回0
  180. $tempvar=preg_match($user_pattern,$this->var_value);
  181. if($tempvar!=1)
  182.  $this->array_errors[$this->var_key]="数据不合法";
  183. }
  184. #########################  类  the end  ################################
  185. }
  186. //<----------------------------作用示例--------------------->
  187. /*
  188. //注意:如果允许一个值为空,则在验证数组前加上i_empty就行了。
  189. //:前面第一个是验证函数,后面的都是参数
  190. $rule_list = array(
  191.  'temp' =>'check_own:"^@([0-9a-z/-_]+/.)+[0-9a-z/-_]+$"',
  192.  'time' => 'i_time',
  193.  'fload' => 'i_float|i_min:1|i_max:10.10|len:0:20',
  194.  'ipadr' => 'i_ip',
  195.  'url' =>'i_url',
  196.     'birthday' => 'i_date',
  197.     'email' => 'i_email|len:1:128',
  198.  'gender' => 'i_int|i_min:1|i_max:20',
  199.     'city' => 'i_string|len:1:32');
  200. $rule_date = array(
  201.  'temp' => '@sina.com',
  202.  'time' => '23:59:00',
  203.  'fload' => '10.0',
  204.  'ipadr' => '251.255.1.1',
  205.  'url' => 'Https://www.gg',
  206.     'birthday' => '2004-5-4',
  207.  'gender' => '15',
  208.     'email' => 'tonerzhang@sohu.com',
  209.     'city' => 'Guangzhou');
  210. $gg=new checker($rule_date);
  211. $gg->check($rule_list);
  212. print_r($gg->array_errors);
  213. */
  214. ?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值