当前位置:Gxlcms > PHP教程 > 一个很好的PHP验证类

一个很好的PHP验证类

时间:2021-07-01 10:21:17 帮助过:1人阅读

  1. /**
  2. * 验证类
  3. *
  4. * @lastmodify 2014-5-16
  5. * @author jy625
  6. */
  7. class VerifyAction{
  8. /**
  9. * 是否为空值
  10. */
  11. public static function isEmpty($str){
  12. $str = trim($str);
  13. return !empty($str) ? true : false;
  14. }
  15. /**
  16. * 数字验证
  17. * param:$flag : int是否是整数,float是否是浮点型
  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. * 名称匹配,如用户名,目录名等
  29. * @param:string $str 要匹配的字符串
  30. * @param:$chinese 是否支持中文,默认支持,如果是匹配文件名,建议关闭此项(false)
  31. * @param:$charset 编码(默认utf-8,支持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. * 邮箱验证
  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验证,纯网址格式,不支持IP验证
  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. * 验证中文
  67. * @param:string $str 要匹配的字符串
  68. * @param:$charset 编码(默认utf-8,支持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验证
  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. * 验证长度
  88. * @param: string $str
  89. * @param: int $type(方式,默认min <= $str <= max)
  90. * @param: int $min,最小值;$max,最大值;
  91. * @param: string $charset 字符
  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. * 验证密码
  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. ,.?\/:;\'\"\d\w]{'.$minLen.','.$maxLen.'}$/';
  116. $v = trim($value);
  117. if(empty($v))
  118. return false;
  119. return preg_match($match,$v);
  120. }
  121. /**
  122. * 验证用户名
  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 = '/^[_\w\d]{'.$minLen.','.$maxLen.'}$/iu';
  132. break;
  133. case 'CN':$match = '/^[_\x{4e00}-\x{9fa5}\d]{'.$minLen.','.$maxLen.'}$/iu';
  134. break;
  135. default:$match = '/^[_\w\d\x{4e00}-\x{9fa5}]{'.$minLen.','.$maxLen.'}$/iu';
  136. }
  137. return preg_match($match,$value);
  138. }
  139. /**
  140. * 验证邮箱
  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. * 匹配日期
  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. * 匹配时间
  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

人气教程排行