我是靠谱客的博主 伶俐战斗机,这篇文章主要介绍PHP验证类的封装与使用方法详解,现在分享给大家,希望可以做个参考。

本文实例讲述了PHP验证类的封装与使用方法。分享给大家供大家参考,具体如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php /** * Created by PhpStorm. * User: jiqing * Date: 18-7-24 * Time: 下午4:36 * 常用验证 */ class Valid { static protected $error; static protected $error_tips = [ 'tel' => '手机号格式有误', 'email' => '邮箱格式有误', 'max_len' => '参数长度不能超过最大长度', 'min_len' => '参数长度不能小于最小长度', 'required' => '缺少参数' ]; // required|max_len,100|min_len,6 public function validate($field, $rules) { $rules = explode('|', $rules); foreach ($rules as $rule) { $method = null; $param = null; // Check if we have rule parameters if (strstr($rule, ',') !== false) { $rule = explode(',', $rule); $method = 'check_'.$rule[0]; $param = $rule[1]; $rule = $rule[0]; } else { $method = 'check_'.$rule; } $method_array = get_class_methods(new Valid()); if (!in_array($method,$method_array)) { self::$error[] = "Method not exist."; } if (!self::$method($field,$param)) { self::$error[] = self::$error_tips[$rule] ? self::$error_tips[$rule] : '参数格式有误'; } } if (count(self::$error) == 0) { return 0; } return self::$error[0]; // 返回第一个错误 } public static function check_required($field) { if (isset($field) && ($field === false || $field === 0 || $field === 0.0 || $field === '0' || !empty($field))) { return true; } else { return false; } } public static function check_tel($field) { if(preg_match("/^1[345678]{1}d{9}$/",$field)){ return true; }else{ return false; } } public static function check_email($field) { if(preg_match("/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+$/",$field)){ return true; }else{ return false; } } public static function check_max_len($field,$param = null) { if (function_exists('mb_strlen')) { if (mb_strlen($field) <= (int) $param) { return true; } else { return false; } } else { if (strlen($field) <= (int) $param) { return true; } else { return false; } } } public static function check_min_len($field,$param = null) { if (function_exists('mb_strlen')) { if (mb_strlen($field) >= (int) $param) { return true; } else { return false; } } else { if (strlen($field) >= (int) $param) { return true; } else { return false; } } } public static function check_regex($field, $param = null) { $regex = $param; if (preg_match($regex, $field)) { return true; } else { return false; } } }

基本满足需求。

复制代码
1
2
3
4
5
vendor('Func.Valid'); if ($res = Valid::validate('152','required|regex,/^1[345678]{1}d{9}$/')) { $this->json->setErr(10001,$res); $this->json->Send(); }

封装很有意思,这个类唯一的亮点,就是可以复合验证。并且支持正则。而且里面的验证方法还可以单独使用。

复制代码
1
2
3
4
5
vendor('Func.Valid'); if (!Valid::check_tel('152')) { $this->json->setErr(10001,'手机号有误'); $this->json->Send(); }

勇敢的封装,利国利民。

继续封装,支持数组传参。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php /** * Created by PhpStorm. * User: jiqing * Date: 18-7-24 * Time: 下午4:36 * 常用验证 */ class Valid { static protected $error; static protected $error_tips = [ 'tel' => '手机号格式有误', 'email' => '邮箱格式有误', 'max_len' => '参数长度不能超过最大长度', 'min_len' => '参数长度不能小于最小长度', 'required' => '缺少参数' ]; /** * @param $validators array array('email' => 'required|valid_email') * @param $input array post数据 * @return string */ public function is_valid($validators, $input) { foreach ($validators as $field => $rules) { if (!isset($input[$field]) || empty($input[$field])) { self::$error[] = "缺少参数"; } $rules = explode('|', $rules); foreach ($rules as $rule) { $method = null; $param = null; // Check if we have rule parameters if (strstr($rule, ',') !== false) { $rule = explode(',', $rule); $method = 'check_'.$rule[0]; $param = $rule[1]; $rule = $rule[0]; } else { $method = 'check_'.$rule; } $method_array = get_class_methods(new Valid()); if (!in_array($method,$method_array)) { self::$error[] = "Method not exist."; } if (!self::$method($input[$field],$param)) { self::$error[] = self::$error_tips[$rule] ? self::$error_tips[$rule] : '参数格式有误'; } } } if (count(self::$error) == 0) { return 0; } return self::$error[0]; // 返回第一个错误 } /** * @param $field string 验证字段 * @param $rules string 验证规则 required|max_len,100|min_len,6 * @return string */ public function validate($field, $rules) { $rules = explode('|', $rules); foreach ($rules as $rule) { $method = null; $param = null; // Check if we have rule parameters if (strstr($rule, ',') !== false) { $rule = explode(',', $rule); $method = 'check_'.$rule[0]; $param = $rule[1]; $rule = $rule[0]; } else { $method = 'check_'.$rule; } $method_array = get_class_methods(new Valid()); if (!in_array($method,$method_array)) { self::$error[] = "Method not exist."; } if (!self::$method($field,$param)) { self::$error[] = self::$error_tips[$rule] ? self::$error_tips[$rule] : '参数格式有误'; } } if (count(self::$error) == 0) { return 0; } return self::$error[0]; // 返回第一个错误 } public static function check_required($field) { if (isset($field) && ($field === false || $field === 0 || $field === 0.0 || $field === '0' || !empty($field))) { return true; } else { return false; } } /** * 简写 * @param $field * @return bool */ public static function check_r($field) { if (isset($field) && ($field === false || $field === 0 || $field === 0.0 || $field === '0' || !empty($field))) { return true; } else { return false; } } public static function check_tel($field) { if(preg_match("/^1[345678]{1}d{9}$/",$field)){ return true; }else{ return false; } } public static function check_email($field) { if(preg_match("/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+$/",$field)){ return true; }else{ return false; } } public static function check_max_len($field,$param = null) { if (function_exists('mb_strlen')) { if (mb_strlen($field) <= (int) $param) { return true; } else { return false; } } else { if (strlen($field) <= (int) $param) { return true; } else { return false; } } } public static function check_min_len($field,$param = null) { if (function_exists('mb_strlen')) { if (mb_strlen($field) >= (int) $param) { return true; } else { return false; } } else { if (strlen($field) >= (int) $param) { return true; } else { return false; } } } public static function check_regex($field, $param = null) { $regex = $param; if (preg_match($regex, $field)) { return true; } else { return false; } } }

使用如下

复制代码
1
2
3
4
5
6
7
8
9
10
11
vendor('Func.Valid'); $validators = [ 'tel' => 'required|tel', 'name' => 'required', 'email' => 'r|email', 'password' => 'r|min_len,6|max_len,12' ]; if ($err = Valid::is_valid($validators,$_POST)) { $this->json->setErr(10001,$err); $this->json->Send(); }

继续优化!支持错误提示中,添加参数。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php /** * Created by PhpStorm. * User: jiqing * Date: 18-7-24 * Time: 下午4:36 * 常用验证 */ class Valid { static protected $error; /** * @param $validators array array('email' => 'required|valid_email') * @param $input array post数据 * @return string */ public function is_valid($validators, $input) { foreach ($validators as $field => $rules) { if (!isset($input[$field]) || empty($input[$field])) { self::$error[] = "缺少参数"; } $rules = explode('|', $rules); foreach ($rules as $rule) { $method = null; $param = null; // Check if we have rule parameters if (strstr($rule, ',') !== false) { $rule = explode(',', $rule); $method = 'check_'.$rule[0]; $param = $rule[1]; $rule = $rule[0]; } else { $method = 'check_'.$rule; } $method_array = get_class_methods(new Valid()); if (!in_array($method,$method_array)) { self::$error[] = "Method not exist."; } if (!self::$method($input[$field],$param)) { self::$error[] = self::get_error_tips($rule,$param); } } } if (count(self::$error) == 0) { return 0; } return self::$error[0]; // 返回第一个错误 } /** * @param $field string 验证字段 * @param $rules string 验证规则 required|max_len,100|min_len,6 * @return string */ public function validate($field, $rules) { $rules = explode('|', $rules); foreach ($rules as $rule) { $method = null; $param = null; // Check if we have rule parameters if (strstr($rule, ',') !== false) { $rule = explode(',', $rule); $method = 'check_'.$rule[0]; $param = $rule[1]; $rule = $rule[0]; } else { $method = 'check_'.$rule; } $method_array = get_class_methods(new Valid()); if (!in_array($method,$method_array)) { self::$error[] = "Method not exist."; } if (!self::$method($field,$param)) { self::$error[] = self::get_error_tips($rule,$param); } } if (count(self::$error) == 0) { return 0; } return self::$error[0]; // 返回第一个错误 } /** * 灵活获取参数 * @param $rule * @param $param */ public static function get_error_tips($rule,$param) { $error_tips = [ 'tel' => '手机号格式有误', 'email' => '邮箱格式有误', 'max_len' => '参数长度不能超过最大长度'.$param, 'min_len' => '参数长度不能小于最小长度'.$param, 'required' => '缺少参数', 'r' => '缺少参数' ]; return $error_tips[$rule] ? $error_tips[$rule] : '参数格式有误'; } public static function check_required($field) { if (isset($field) && ($field === false || $field === 0 || $field === 0.0 || $field === '0' || !empty($field))) { return true; } else { return false; } } /** * 简写 * @param $field * @return bool */ public static function check_r($field) { if (isset($field) && ($field === false || $field === 0 || $field === 0.0 || $field === '0' || !empty($field))) { return true; } else { return false; } } public static function check_tel($field) { if(preg_match("/^1[345678]{1}d{9}$/",$field)){ return true; }else{ return false; } } public static function check_email($field) { if(preg_match("/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+$/",$field)){ return true; }else{ return false; } } public static function check_max_len($field,$param = null) { if (function_exists('mb_strlen')) { if (mb_strlen($field) <= (int) $param) { return true; } else { return false; } } else { if (strlen($field) <= (int) $param) { return true; } else { return false; } } } public static function check_min_len($field,$param = null) { if (function_exists('mb_strlen')) { if (mb_strlen($field) >= (int) $param) { return true; } else { return false; } } else { if (strlen($field) >= (int) $param) { return true; } else { return false; } } } public static function check_regex($field, $param = null) { $regex = $param; if (preg_match($regex, $field)) { return true; } else { return false; } } }

PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:

JavaScript正则表达式在线测试工具:
http://tools.uoften.com/regex/javascript

正则表达式在线生成工具:
http://tools.uoften.com/regex/create_reg

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php正则表达式用法总结》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

最后

以上就是伶俐战斗机最近收集整理的关于PHP验证类的封装与使用方法详解的全部内容,更多相关PHP验证类内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(103)

评论列表共有 0 条评论

立即
投稿
返回
顶部