首页 > 开发 > PHP > 正文

php可扩展的验证类实例(可对邮件、手机号、URL等验证)

2024-05-04 23:37:34
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了php可扩展的验证类,实例分析了php针对邮件、手机号、URL等常用的验证技巧,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了php可扩展的验证类。分享给大家供大家参考。具体分析如下:

这里介绍一个可扩展的php验证类,

类里面可以的各类验证可自行调整实现,现在为基本实现方式。

需要添加规则的话, 直接定义方法,方法名即为规则名称。具体参考使用方法。

 

 
  1. require_once('./Validator.class.php'); 
  2. $data = array( 
  3. 'nickname' => 'heno' , 
  4. 'realname' => 'steven', 
  5. 'age' => 25, 
  6. 'mobile' => '1521060426'); 
  7. $validator = new Validator($data); 
  8. $validator->setRule('nickname', 'required'); 
  9. $validator->setRule('realname', array('length' => array(1,6), 'required')); 
  10. $validator->setRule('age', array('required', 'digit')); 
  11. $validator->setRule('mobile', array('mobile')); 
  12. $result = $validator->validate(); 
  13. var_dump($result); 
  14. var_dump($validator->getResultInfo()); 

Validator.class.php文件如下:

 

 
  1. <?php 
  2. /** 
  3. * Validator 数据验证类 
  4. * @package library 
  5. * @category library 
  6. * @author Steven 
  7. * @version 1.0 
  8. */ 
  9. /** 
  10. * Validator 数据验证类 
  11. * @package library 
  12. * @category library 
  13. * @author Steven 
  14. * @version 1.0 
  15. */ 
  16. class Validator { 
  17. /** 
  18. * 待校验数据 
  19. * @var array 
  20. */ 
  21. private $_data; 
  22. /** 
  23. * 校验规则 
  24. * @var array 
  25. */ 
  26. private $_ruleList = null; 
  27. /** 
  28. * 校验结果 
  29. * @var bool 
  30. */ 
  31. private $_result = null; 
  32. /** 
  33. * 校验数据信息 
  34. * @var array 
  35. */ 
  36. private $_resultInfo = array(); 
  37. /** 
  38. * 构造函数 
  39. * @param array $data 待校验数据 
  40. */ 
  41. public function __construct($data = null) 
  42. { 
  43. if ($data) { 
  44. $this->_data = $data; 
  45. } 
  46. } 
  47. /** 
  48. * 设置校验规则 
  49. * @param string $var 带校验项key 
  50. * @param mixed $rule 校验规则 
  51. * @return void 
  52. */ 
  53. public function setRule($var, $rule) 
  54. { 
  55. $this->_ruleList[$var] = $rule; 
  56. } 
  57. /** 
  58. * 检验数据 
  59. * @param array $data  
  60. * <code> 
  61. * $data = array('nickname' => 'heno' , 'realname' => 'steven', 'age' => 25); 
  62. * $validator = new Validator($data); 
  63. * $validator->setRule('nickname', 'required'); 
  64. * $validator->setRule('realname', array('lenght' => array(1,4), 'required')); 
  65. * $validator->setRule('age', array('required', 'digit')); 
  66. * $result = $validator->validate(); 
  67. * var_dump($validator->getResultInfo()); 
  68. * </code> 
  69. * @return bool 
  70. */ 
  71. public function validate($data = null) 
  72. { 
  73. $result = true; 
  74. /* 如果没有设置校验规则直接返回 true */ 
  75. if ($this->_ruleList === null || !count($this->_ruleList)) { 
  76. return $result; 
  77. } 
  78. /* 已经设置规则,则对规则逐条进行校验 */ 
  79. foreach ($this->_ruleList as $ruleKey => $ruleItem) { 
  80. /* 如果检验规则为单条规则 */ 
  81. if (!is_array($ruleItem)) { 
  82. $ruleItem = trim($ruleItem); 
  83. if (method_exists($this, $ruleItem)) { 
  84. /* 校验数据,保存校验结果 */ 
  85. $tmpResult = $this->$ruleItem($ruleKey); 
  86. if (!$tmpResult) { 
  87. $this->_resultInfo[$ruleKey][$ruleItem] = $tmpResult; 
  88. $result = false; 
  89. } 
  90. } 
  91. continue; 
  92. } 
  93. /* 校验规则为多条 */ 
  94. foreach ($ruleItem as $ruleItemKey => $rule) { 
  95. if (!is_array($rule)) { 
  96. $rule = trim($rule); 
  97. if (method_exists($this, $rule)) { 
  98. /* 校验数据,设置结果集 */ 
  99. $tmpResult = $this->$rule($ruleKey); 
  100. if (!$tmpResult) { 
  101. $this->_resultInfo[$ruleKey][$rule] = $tmpResult; 
  102. $result = false; 
  103. } 
  104. } 
  105. } else { 
  106. if (method_exists($this, $ruleItemKey)) { 
  107. /* 校验数据,设置结果集 */ 
  108. $tmpResult = $this->$ruleItemKey($ruleKey, $rule); 
  109. if (!$tmpResult) { 
  110. $this->_resultInfo[$ruleKey][$ruleItemKey] = $tmpResult; 
  111. $result = false; 
  112. } 
  113. } 
  114. } 
  115. } 
  116. } 
  117. return $result; 
  118. } 
  119. /** 
  120. * 获取校验结果数据 
  121. * @return [type] [description] 
  122. */ 
  123. public function getResultInfo() 
  124. { 
  125. return $this->_resultInfo; 
  126. } 
  127. /** 
  128. * 校验必填参数 
  129. * @param string $varName 校验项 
  130. * @return bool 
  131. */ 
  132. public function required($varName)  
  133. { 
  134. $result = false; 
  135. if (is_array($this->_data) && isset($this->_data[$varName])) { 
  136. $result = true; 
  137. } 
  138. return $result; 
  139. } 
  140. /** 
  141. * 校验参数长度 
  142. *  
  143. * @param string $varName 校验项 
  144. * @param array $lengthData array($minLen, $maxLen) 
  145. * @return bool 
  146. */ 
  147. public function length($varName, $lengthData) 
  148. { 
  149. $result = true; 
  150. /* 如果该项没有设置,默认为校验通过 */ 
  151. if ($this->required($varName)) { 
  152. $varLen = mb_strlen($this->_data[$varName]); 
  153. $minLen = $lengthData[0]; 
  154. $maxLen = $lengthData[1]; 
  155. if ($varLen < $minLen || $varLen > $maxLen) { 
  156. $result = true; 
  157. } 
  158. } 
  159. return $result; 
  160. } 
  161. /** 
  162. * 校验邮件 
  163. * @param string $varName 校验项 
  164. * @return bool 
  165. */ 
  166. public function email($varName) 
  167. { 
  168. $result = true; 
  169. /* 如果该项没有设置,默认为校验通过 */ 
  170. if ($this->required($varName)) { 
  171. $email = trim($this->_data[$varName]); 
  172. if (preg_match('/^[-/w]+?@[-/w.]+?$/', $email)) { 
  173. $result = false; 
  174. } 
  175. } 
  176. return $result; 
  177. } 
  178. /** 
  179. * 校验手机 
  180. * @param string $varName 校验项 
  181. * @return bool 
  182. */ 
  183. public function mobile($varName) 
  184. { 
  185. $result = true; 
  186. /* 如果该项没有设置,默认为校验通过 */ 
  187. if ($this->required($varName)) { 
  188. $mobile = trim($this->_data[$varName]); 
  189. if (!preg_match('/^1[3458]/d{10}$/', $mobile)) { 
  190. $result = false; 
  191. } 
  192. } 
  193. return $result; 
  194. } 
  195. /** 
  196. * 校验参数为数字 
  197. * @param string $varName 校验项 
  198. * @return bool 
  199. */ 
  200. public function digit($varName) 
  201. { 
  202. $result = false; 
  203. if ($this->required($varName) && is_numeric($this->_data[$varName])) { 
  204. $result = true; 
  205. } 
  206. return $result; 
  207. } 
  208. /** 
  209. * 校验参数为身份证 
  210. * @param string $varName 校验项 
  211. * @return bool 
  212. */ 
  213. public function ID($ID) 
  214. { 
  215. } 
  216. /** 
  217. * 校验参数为URL 
  218. * @param string $varName 校验项 
  219. * @return bool 
  220. */ 
  221. public function url($url) 
  222. { 
  223. $result = true; 
  224. /* 如果该项没有设置,默认为校验通过 */ 
  225. if ($this->required($varName)) { 
  226. $url = trim($this->_data[$varName]); 
  227. if(!preg_match('/^(http[s]?::)?/w+?(/./w+?)$/', $url)) { 
  228. $result = false; 
  229. } 
  230. } 
  231. return $result; 
  232. } 
  233. } 
  234. ?> 

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表