一個很好的PHP驗證類

來源:互聯網
上載者:User
  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
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.