分享一个很好用的函数

浏览:555 发布日期:2015/05/25 分类:技术分享 关键字: php
快速取得表单提交数据,并且可作简单数据验证    /**
     * 取得提交的数据
     * @param unknown $data
     * @param string $method
     * @return mixed
     * $test = input(array('platform'=>'平台', 'bankcode'=>array('without', 1001), 'money'=>array('egt', 100, '充值至少100元')), 'post');
     * $test = input('platform', 'bankcode', 'money', 'get');
     */
    function input($data, $method='post'){

        if(is_string($data)){
            $data = func_get_args(); $method = array_pop($data);
        }
        if(empty($method)) $method = 'param';
        foreach($data as $field => $options){
            
            if(is_numeric($field)){
                $field = $options; $options = array('without', '');
            }
            if(!is_array($options)){
                $options = array('neq', '', $options .'不能为空');
            }
            $return[$field] = $value = I($method. '.' .$field, $options[1]);
            if(empty($options[2])) $options[2] = '参数验证不合法';
            $info = '';
             switch($options[0]){
                case 'eq' : if($value != $options[1]) $info = $options[2];
                    break;
                case 'neq': if($value == $options[1]) $info = $options[2];
                    break;
                case 'lt' : if($value >= $options[1]) $info = $options[2];
                    break;
                case 'gt' : if($value <= $options[1]) $info = $options[2];
                    break;
                case 'elt': if($value > $options[1]) $info = $options[2];
                    break;
                case 'egt': if($value < $options[1]) $info = $options[2];
                    break;
                case 'without':
                    break;
                default:  $info = '比较类型不合法';
            }
            //if(!empty($info)) $return['errors'][$field] = $info;//所有错误消息
        }
        
        if(!empty($info)) $return['error'] = $info;
        return $return;
    }
最佳答案
评论( 相关
后面还有条评论,点击查看>>