php 验证器 判断输入是否符合要求

<?php
#保留允许的键值
if(!function_exists('keep_allow_array')){
    function keep_allow_array($data,$allow_array){
        if(!is_array($data) || !is_array($allow_array))return $data;
        foreach ($data as $key => $value) {
            if(!in_array($key, $allow_array))unset($data[$key]);
        }
        return $data;
    }
}



if(!function_exists('isPhone_check')) {
    #检查是不是手机号:请输入正确的手机号
    function isPhone_check($key,$data,$value) {
        if(before_function_check($key,$data,$value)){
            return ['code'=>true];
        }
        if(preg_match("/^1[34578]{1}\d{9}$/",$data[$key]))
            return ['code'=>true];
        return ['code'=>false,'msg'=>$value];
    }
}
#检查是不是数字包括小数点
if(!function_exists('checkNumber_check')) {
    #检查是不是数字包括小数点
    function checkNumber_check($key,$data,$value) {
        if(before_function_check($key,$data,$value)){
            return ['code'=>true];
        }
        #判断是否是数字或判断小数点最多位数
        $exp = explode('|',$value);
        if(count($exp)==1){
            if (preg_match('/^[0-9]+(.[0-9]+)?$/',$data[$key]))
                return ['code'=>true];
            return ['code'=>false,'msg'=>$value];            
        }else{
            if (preg_match('/^[0-9]+(.[0-9]{0,'.$exp[0].'})?$/',$data[$key]))
                return ['code'=>true];
            return ['code'=>false,'msg'=>$exp[1]];  
        }


    }
}
#检查是不是整型
if(!function_exists('checkInt_check')) {
    #检查是不是数字包括小数点
    function checkInt_check($key,$data,$value) {
        if(before_function_check($key,$data,$value)){
            return ['code'=>true];
        }
        if (preg_match('/^[0-9]+$/',$data[$key]))
                return ['code'=>true];
        return ['code'=>false,'msg'=>$value];
    }
}

#该字段不许存在
if(!function_exists('must_check')) {
    #检测字段必须存在
    function must_check($key,$data,$value) {
        if(before_function_check($key,$data,$value)){
            return ['code'=>true];
        }
        if($data[$key]=='')
            return ['code'=>false,'msg'=>$value];
        return ['code'=>true];
    }
}
#检查是不是在数组里
if(!function_exists('checkInArr_check')) {
    #检测是否在一个数组内:1,2,3,4,5|提示信息
    function checkInArr_check($key,$data,$value) {
        if(before_function_check($key,$data,$value)){
            return ['code'=>true];
        }
        $exp = explode('|',$value);
        if(count($exp) != 2) exit('格式不对例如:(1,2,3,4,5|提示信息)');
        
        //生成数组
        $arr = explode(',',$exp[0]);
        if(in_array($data[$key],$arr))
            return ['code'=>true];
        return ['code'=>false,'msg'=>$exp[1]];
    }
}
#检查是不是不在数组里
if(!function_exists('checkNotInArr_check')) {
    #检测是不是不在一个数组内:1,2,3,4,5|提示信息
    function checkNotInArr_check($key,$data,$value) {
        if(before_function_check($key,$data,$value)){
            return ['code'=>true];
        }
        $exp = explode('|',$value);
        if(count($exp) != 2) exit('格式不对例如:(1,2,3,4,5|提示信息)');
        //生成数组
        $arr = explode(',',$exp[0]);
        if(in_array($data[$key],$arr))
                return ['code'=>false,'msg'=>$exp[1]];
        return ['code'=>true];
    }
}
#不允许大于某个长度:5|提示信息
if(!function_exists('checkStrGtLen_check')) {
    function checkStrGtLen_check($key,$data,$value) {
        if(before_function_check($key,$data,$value)){
            return ['code'=>true];
        }
        $exp = explode('|',$value);
        if(count($exp) != 2) exit('格式不对例如:(300|提示信息)');
        $allowStrLen = $exp[0];
        if(mb_strlen($data[$key],"utf-8")>(int)$allowStrLen)
            return ['code'=>false,'msg'=>$exp[1]];
        return ['code'=>true];
    }
}
#不允许大于某个整数
if(!function_exists('checkIntGtLen_check')) {
    #不允许大于某个数字比如年龄:5|提示信息
    function checkIntGtLen_check($key,$data,$value) {
        if(before_function_check($key,$data,$value)){
            return ['code'=>true];
        }
        $exp = explode('|',$value);
        if(count($exp) != 2) exit('格式不对例如:(300|提示信息)');
        
        $allowStrLen = $exp[0];
        if((int)$data[$key]>(int)$allowStrLen)
            return ['code'=>false,'msg'=>$exp[1]];
        return ['code'=>true];
    }
}
#字符串长度不允许小于某个长度
if(!function_exists('checkStrLtLen_check')) {
    #不允许小于某个长度5|提示信息
    function checkStrLtLen_check($key,$data,$value) {
        if(before_function_check($key,$data,$value)){
            return ['code'=>true];
        }
        $exp = explode('|',$value);
        if(count($exp) != 2) exit('格式不对例如:(300|提示信息)');
        $allowStrLen = $exp[0];
        if(mb_strlen($data[$key],"utf-8")<(int)$allowStrLen)
            return ['code'=>false,'msg'=>$exp[1]];
        return ['code'=>true];
    }
}
#整型不允许小于某个长度
if(!function_exists('checkIntLtLen_check')) {
    #不允许数字小于某个数比如判断年龄 5|提示信息
    function checkIntLtLen_check($key,$data,$value) {
        if(before_function_check($key,$data,$value)){
            return ['code'=>true];
        }
        $exp = explode('|',$value);
        if(count($exp) != 2) exit('格式不对例如:(300|提示信息)');
        $allowStrLen = $exp[0];
        if((int)$data[$key]<(int)$allowStrLen)
            return ['code'=>false,'msg'=>$exp[1]];
        return ['code'=>true];
    }
}
#字符串长度是否在某个区间
if(!function_exists('checkStrBetweenLen_check')) {
    #数符长度必须在两个数字中间 5,20|提示信息
    function checkStrBetweenLen_check($key,$data,$value) {
        if(before_function_check($key,$data,$value)){
            return ['code'=>true];
        }
        $exp = explode('|',$value);
        if(count($exp) != 2) exit('格式不对例如:(10,20|提示信息)');
        //生成数组
        $arr = explode(',',$exp[0]);
        if(count($arr)!=2) exit('格式不对例如:(10,20|提示信息)');
        sort($arr);
        if((mb_strlen($data[$key],"utf-8")<(int)$arr[0]) || (mb_strlen($data[$key],"utf-8")>(int)$arr[1]))
                return ['code'=>false,'msg'=>$exp[1]];
        return ['code'=>true];
    }
}
#某个数字是否在一定范围内
if(!function_exists('checkIntBetweenLen_check')) {
    #数字必须在两个数字中间 5,20|提示信息
    function checkIntBetweenLen_check($key,$data,$value) {
        if(before_function_check($key,$data,$value)){
            return ['code'=>true];
        }
        $exp = explode('|',$value);
        if(count($exp) != 2) exit('格式不对例如:(10,20|提示信息)');
        //生成数组
        $arr = explode(',',$exp[0]);
        if(count($arr)!=2)exit('格式不对例如:(10,20|提示信息)');
        sort($arr);
        if(((int)$data[$key]<(int)$arr[0]) || ((int)$data[$key]>(int)$arr[1]))
            return ['code'=>false,'msg'=>$exp[1]];
        return ['code'=>true];
    }
}

#当某个字段存在或某个字段的值等于某值时验证
if(!function_exists('requireWith_check')){
    function requireWith_check($key,$data,$value){
        // price|店铺名称必须填写#字段存在验证
        // price:1|店铺名称必须填写 某个字段的值验证
        $exp = explode('|',$value);
        if(count($exp) != 2) exit('格式不对例如:((price:1)或(price)|提示信息)');
        $arr = explode(':',$exp[0]);
        $count=count($arr);
        if(($count !=1) && ($count !=2)){
            exit('格式不对例如:((price:1)或(price)|提示信息)');
        }

        #键值存在
        if($count==1){
            if(isset($data[$arr[0]])){
                if(!isset($data[$key])){
                    return ['code'=>false,'msg'=>$exp[1]];
                }else{
                    return ['code'=>true];
                }
            }else{
                 return ['code'=>true];
            }
        }
        #等于某值
        if($count==2){
            if(isset($data[$arr[0]])){
                if(isset($data[$key]) && ($data[$key]==$arr[1])){
                    return ['code'=>true];
                }else{
                    return ['code'=>false,'msg'=>$exp[1]];
                }
            }else{
                return ['code'=>true];
            }
        }

        

    }
}


function before_function_check($key,$data,$value){
    if(!isset($data[$key])){
        return true;
    }
    return false;
    

}






if(!function_exists('checkField')) {
    /** 
     * $rule 规则
     * $data $_POST所有数据
     * $if true 直接返回接口  false为返回判断数据
     */
    function checkField($rule,$data,$if=true) {
        $return_data = ['code'=>true];
        foreach($rule as $k => $v) {
            #判断值是否存在
            if(!isset($data[$k])) {
                if(isset($v['must'])){
                    $return_data =  ['code'=>false,'msg'=>$v['must']];
                    if($if)break;
                    return $return_data;
                }
            }
            foreach ($v as $key => $value) {
                #防止于系统函数冲突
                $function = $key.'_check';
                $reqdata = $function($k,$data,$value);
                if(!$reqdata['code']) {
                    $return_data = $reqdata;
                    if($if)break 2;
                    return $return_data;
                }
            }
        }
        if($if){
            if(!$return_data['code'])exit_data($return_data['msg']);
        }else{
            return $return_data;
        }
    }
}


$rule=[
    'id'=>['must'=>'为什么没id呢','checkNumber'=>'id是数字',],
    'contactPersonnel'=>['must'=>'联系人必须填写','checkStrBetweenLen'=>'2,5|名字不能太长',],
    'phone'=>['must'=>'手机号必须填写','isPhone'=>'请填写正确的手机号',],
    'number'=>['must'=>'商品数量必须填写','checkNumber'=>'购买件数必须是数字',],
    'remark'=>['checkStrBetweenLen'=>'5,100|备注必须在5~200个字符之内',],
];


checkField($rule,$_post数据...);
# https://files-cdn.cnblogs.com/files/chengfengchi/%E8%BF%87%E6%BB%A4%E8%AF%B7%E6%B1%82%E6%95%B0%E6%8D%AE.zip  下载生成器

 

posted @ 2020-04-08 17:59  酷酷的城池  阅读(691)  评论(0编辑  收藏  举报