Home  >  Article  >  Backend Development  >  5

5

WBOY
WBOYOriginal
2016-07-25 08:47:29883browse
ddd
  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='/^[\~!@#$%^&*()-_=+|{}[],.?/:;'"dw]{'.$minLen.','.$maxLen.'}$/';
  115. $v = trim($value);
  116. if(empty($v))
  117. return false;
  118. return preg_match($match,$v);
  119. }
  120. /**
  121. * Verify username
  122. * @param string $value
  123. * @param int $length
  124. * @return boolean
  125. */
  126. public static function isNames($value, $minLen=2, $maxLen=16, $charset='ALL'){
  127. if(empty($value))
  128. return false;
  129. switch($charset){
  130. case 'EN': $match = '/^[_wd]{'.$minLen.','.$maxLen.'}$/iu';
  131. break;
  132. case 'CN':$match = '/^[_x{4e00}-x{9fa5}d]{'.$minLen.','.$maxLen.'}$/iu';
  133. break;
  134. default:$match = '/^[_wdx{4e00}-x{9fa5}]{'.$minLen.','.$maxLen.'}$/iu';
  135. }
  136. return preg_match($match,$value);
  137. }
  138. /**
  139. * Verification email
  140. * @param string $value
  141. */
  142. public static function checkZip($str){
  143. if(strlen($str)!=6){
  144. return false;
  145. }
  146. if(substr($str,0,1)==0){
  147. return false;
  148. }
  149. return true;
  150. }
  151. /**
  152. * Match date
  153. * @param string $value
  154. */
  155. public static function checkDate($str){
  156. $dateArr = explode("-", $str);
  157. if (is_numeric($dateArr[0]) && is_numeric($dateArr[1]) && is_numeric($dateArr[2])) {
  158. if (($dateArr[0] >= 1000 && $timeArr[0] <= 10000) && ($dateArr[1] >= 0 && $dateArr[1] <= 12) && ($dateArr[2] >= 0 && $dateArr[2] <= 31))
  159. return true;
  160. else
  161. return false;
  162. }
  163. return false;
  164. }
  165. /**
  166. * Matching time
  167. * @param string $value
  168. */
  169. public static function checkTime($str){
  170. $timeArr = explode(":", $str);
  171. if (is_numeric($timeArr[0]) && is_numeric($timeArr[1]) && is_numeric($timeArr[2])) {
  172. if (($timeArr[0] >= 0 && $timeArr[0] <= 23) && ($timeArr[1] >= 0 && $timeArr[1] <= 59) && ($timeArr[2] >= 0 && $timeArr[2] <= 59))
  173. return true;
  174. else
  175. return false;
  176. }
  177. return false;
  178. }
  179. }
复制代码
  1. dddd
复制代码


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
Previous article:PHP listening SocketNext article:PHP listening Socket