Home  >  Article  >  Backend Development  >  A good PHP validation class

A good PHP validation class

WBOY
WBOYOriginal
2016-07-25 08:42:57771browse
  1. /**
  2. * Verification class
  3. *
  4. * @lastmodify 2014-5-16
  5. * @author jy625
  6. */
  7. class VerifyAction{
  8. /**
  9. * Whether it is a null value
  10. */
  11. public static function isEmpty($str){
  12. $str = trim($str);
  13. return !empty($str) ? true : false;
  14. }
  15. /**
  16. * Number verification
  17. * param:$flag: whether int is an integer and float is a floating point type
  18. */
  19. public static function isNum($str,$flag = 'float'){
  20. if(!self::isEmpty($str)) return false;
  21. if(strtolower($flag) == 'int'){
  22. return ((string)(int)$str === (string)$str) ? true : false;
  23. }else{
  24. return ((string)(float)$str === (string)$str) ? true : false;
  25. }
  26. }
  27. /**
  28. * Name matching, such as user name, directory name, etc.
  29. * @param:string $str The string to be matched
  30. * @param:$chinese Whether to support Chinese, supported by default. If it is matching file names, it is recommended to turn this off ( false)
  31. * @param:$charset encoding (default utf-8, supports gb2312)
  32. */
  33. public static function isName($str,$chinese = true,$charset = 'utf-8'){
  34. if(!self::isEmpty($str)) return false;
  35. if($chinese){
  36. $match = (strtolower($charset) == 'gb2312') ? "/^[".chr(0xa1)."-".chr(0xff)."A-Za-z0-9_-]+$/" : "/^[x{4e00}-x{9fa5}A-Za-z0-9_]+$/u";
  37. }else{
  38. $match = '/^[A-za-z0-9_-]+$/';
  39. }
  40. return preg_match($match,$str) ? true : false;
  41. }
  42. /**
  43. * Email verification
  44. */
  45. public static function isEmail($str){
  46. if(!self::isEmpty($str)) return false;
  47. return preg_match("/([a-z0-9]*[-_.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[.][a-z]{2,3}([.][a-z]{2})?/i",$str) ? true : false;
  48. }
  49. //手机号码验证
  50. public static function isMobile($str){
  51. $exp = "/^13[0-9]小贝[0-9]{8}$|15[012356789]小贝[0-9]{8}$|18[012356789]小贝[0-9]{8}$|14[57]小贝[0-9]$/";
  52. if(preg_match($exp,$str)){
  53. return true;
  54. }else{
  55. return false;
  56. }
  57. }
  58. /**
  59. * URL verification, pure URL format, does not support IP verification
  60. */
  61. public static function isUrl($str){
  62. if(!self::isEmpty($str)) return false;
  63. return preg_match('#(http|https|ftp|ftps)://([w-]+.)+[w-]+(/[w-./?%&=]*)?#i',$str) ? true : false;
  64. }
  65. /**
  66. * Verify Chinese
  67. * @param:string $str The string to be matched
  68. * @param:$charset encoding (default utf-8, support gb2312)
  69. */
  70. public static function isChinese($str,$charset = 'utf-8'){
  71. if(!self::isEmpty($str)) return false;
  72. $match = (strtolower($charset) == 'gb2312') ? "/^[".chr(0xa1)."-".chr(0xff)."]+$/"
  73. : "/^[x{4e00}-x{9fa5}]+$/u";
  74. return preg_match($match,$str) ? true : false;
  75. }
  76. /**
  77. * UTF-8 validation
  78. */
  79. public static function isUtf8($str){
  80. if(!self::isEmpty($str)) return false;
  81. return (preg_match("/^([".chr(228)."-".chr(233)."]小贝[".chr(128)."-".chr(191)."]小贝[".chr(128)."-".chr(191)."]小贝)小贝/",$word)
  82. == true || preg_match("/([".chr(228)."-".chr(233)."]小贝[".chr(128)."-".chr(191)."]小贝[".chr(128)."-".chr(191)."]小贝)小贝$/",$word)
  83. == true || preg_match("/([".chr(228)."-".chr(233)."]小贝[".chr(128)."-".chr(191)."]小贝[".chr(128)."-".chr(191)."]小贝){2,}/",$word)
  84. == true) ? true : false;
  85. }
  86. /**
  87. * Verification length
  88. * @param: string $str
  89. * @param: int $type (method, default min <= $str <= max)
  90. * @param: int $min, minimum value; $max, Maximum value;
  91. * @param: string $charset character
  92. */
  93. public static function length($str,$type=3,$min=0,$max=0,$charset = 'utf-8'){
  94. if(!self::isEmpty($str)) return false;
  95. $len = mb_strlen($str,$charset);
  96. switch($type){
  97. case 1: //只匹配最小值
  98. return ($len >= $min) ? true : false;
  99. break;
  100. case 2: //只匹配最大值
  101. return ($max >= $len) ? true : false;
  102. break;
  103. default: //min <= $str <= max
  104. return (($min <= $len) && ($len <= $max)) ? true : false;
  105. }
  106. }
  107. /**
  108. * Verification password
  109. * @param string $value
  110. * @param int $length
  111. * @return boolean
  112. */
  113. public static function isPWD($value,$minLen=6,$maxLen=16){
  114. $match='/^[\~!@#$%^&*()-_=+|{}
  115. ,.?/:;'"dw]{'.$minLen.','.$maxLen.'}$/';
  116. $v = trim($value);
  117. if(empty($v))
  118. return false;
  119. return preg_match($match,$v);
  120. }
  121. /**
  122. * Verify username
  123. * @param string $value
  124. * @param int $length
  125. * @return boolean
  126. */
  127. public static function isNames($value, $minLen=2, $maxLen=16, $charset='ALL'){
  128. if(empty($value))
  129. return false;
  130. switch($charset){
  131. case 'EN': $match = '/^[_wd]{'.$minLen.','.$maxLen.'}$/iu';
  132. break;
  133. case 'CN':$match = '/^[_x{4e00}-x{9fa5}d]{'.$minLen.','.$maxLen.'}$/iu';
  134. break;
  135. default:$match = '/^[_wdx{4e00}-x{9fa5}]{'.$minLen.','.$maxLen.'}$/iu';
  136. }
  137. return preg_match($match,$value);
  138. }
  139. /**
  140. * Verification email
  141. * @param string $value
  142. */
  143. public static function checkZip($str){
  144. if(strlen($str)!=6){
  145. return false;
  146. }
  147. if(substr($str,0,1)==0){
  148. return false;
  149. }
  150. return true;
  151. }
  152. /**
  153. * Match date
  154. * @param string $value
  155. */
  156. public static function checkDate($str){
  157. $dateArr = explode("-", $str);
  158. if (is_numeric($dateArr[0]) && is_numeric($dateArr[1]) && is_numeric($dateArr[2])) {
  159. if (($dateArr[0] >= 1000 && $timeArr[0] <= 10000) && ($dateArr[1] >= 0 && $dateArr[1] <= 12) && ($dateArr[2] >= 0 && $dateArr[2] <= 31))
  160. return true;
  161. else
  162. return false;
  163. }
  164. return false;
  165. }
  166. /**
  167. * Matching time
  168. * @param string $value
  169. */
  170. public static function checkTime($str){
  171. $timeArr = explode(":", $str);
  172. if (is_numeric($timeArr[0]) && is_numeric($timeArr[1]) && is_numeric($timeArr[2])) {
  173. if (($timeArr[0] >= 0 && $timeArr[0] <= 23) && ($timeArr[1] >= 0 && $timeArr[1] <= 59) && ($timeArr[2] >= 0 && $timeArr[2] <= 59))
  174. return true;
  175. else
  176. return false;
  177. }
  178. return false;
  179. }
  180. }
复制代码

很好, PHP


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn