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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值