>백엔드 개발 >PHP 튜토리얼 >좋은 PHP 유효성 검사 클래스

좋은 PHP 유효성 검사 클래스

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB원래의
2016-07-25 08:42:57833검색
아주 좋아요, PHP
  1. /**
  2. * 검증 클래스
  3. *
  4. * @lastmodify 2014-5-16
  5. * @author jy625
  6. */
  7. class verifyAction{
  8. /**
  9. * null 값인지 여부
  10. */
  11. 공개 정적 함수 isEmpty($str){
  12. $str = 트림($str);
  13. return !empty($str) ? 사실 : 거짓;
  14. }
  15. /**
  16. * 숫자 확인
  17. * param:$flag: int가 정수이고 float가 부동 소수점 유형인지 여부
  18. */
  19. 공개 정적 함수 isNum($str,$flag = 'float'){
  20. if(!self::isEmpty($str)) return 거짓;
  21. if(strtolower($flag) == 'int'){
  22. return ((string)(int)$str === (string)$str) ? 사실 : 거짓;
  23. }else{
  24. return ((string)(float)$str === (string)$str) ? 사실 : 거짓;
  25. }
  26. }
  27. /**
  28. * 사용자 이름, 디렉터리 이름 등 이름 매칭
  29. * @param:string $str 매칭할 문자열
  30. * @param:$chinese 중국어 지원 여부, 기본적으로 지원, 파일 이름과 일치하는 경우 끄는 것이 좋습니다(false).
  31. * @param:$charset 인코딩(기본값 utf-8, gb2312 지원)
  32. */
  33. 공개 정적 함수 isName($str,$chinese = true,$charset = 'utf-8'){
  34. if( !self::isEmpty($str)) 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) ? 사실 : 거짓;
  41. }
  42. /**
  43. * 이메일 확인
  44. */
  45. 공개 정적 함수 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) ? 사실 : 거짓;
  48. }
  49. //手机号码验证
  50. 공개 정적 함수 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. false를 반환합니다.
  56. }
  57. }
  58. /**
  59. * URL 확인은 순수 URL 형식으로, IP 확인을 지원하지 않습니다.
  60. */
  61. 공개 정적 함수 isUrl($str){
  62. if(!self::isEmpty($str)) return false;
  63. return preg_match('#(http|https|ftp|ftps)://([w-] .) [w-] (/[w-./?%&=]*)?#i', $str) ? 사실 : 거짓;
  64. }
  65. /**
  66. * 중국어 확인
  67. * @param:string $str 일치시킬 문자열
  68. * @param:$charset 인코딩(기본값 utf-8, gb2312 지원)
  69. */
  70. 공개 정적 함수 isChina($str,$charset = 'utf-8'){
  71. if(!self::isEmpty($str) ) 거짓을 반환합니다.
  72. $match = (strtolower($charset) == 'gb2312') ? "/^[".chr(0xa1)."-".chr(0xff)."] $/"
  73. : "/^[x{4e00}-x{9fa5}] $/u";
  74. preg_match($match,$str)를 반환합니까? 사실 : 거짓;
  75. }
  76. /**
  77. * UTF-8 유효성 검사
  78. */
  79. 공개 정적 함수 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) ? 사실 : 거짓;
  85. }
  86. /**
  87. * 검증 길이
  88. * @param: string $str
  89. * @param: int $type (모드, 기본값 min <= $str <= max)
  90. * @param: int $ 최소값, 최소값, 최대값
  91. * @param: 문자열 $charset 문자
  92. */
  93. 공개 정적 함수 길이($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. 사례 1: //다음과만 일치합니다. 가장 작은 값
  98. return ($len >= $min) ? true : false
  99. break
  100. 사례 2: //최대값만 일치
  101. return ($max >= $len ) ? true: false;
  102. break
  103. 기본값: //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. 공개 정적 함수 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. * 사용자 이름 확인
  123. * @param string $value
  124. * @param int $length
  125. * @return boolean
  126. */
  127. 공개 정적 함수 isNames($value, $minLen=2, $maxLen=16, $charset='ALL'){
  128. if( 비어 있음( $value))
  129. return false
  130. switch($charset){
  131. 케이스 'EN': $match = '/^[_wd]{'.$minLen.','.$maxLen .' }$/iu';
  132. break
  133. 케이스 '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. * 확인 이메일
  141. * @param string $value
  142. */
  143. 공개 정적 함수 checkZip($str)
  144. if(strlen($str)!=6){
  145. return false
  146. }
  147. if(substr($str,0,1)==0){
  148. return false;
  149. }
  150. true를 반환합니다.
  151. }
  152. /**
  153. * 일치 날짜
  154. * @param string $value
  155. */
  156. 공개 정적 함수 checkDate($str){
  157. $dateArr =explore("-", $ 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. true를 반환합니다.
  161. else
  162. false를 반환합니다.
  163. }
  164. /**
  165. * 매칭 시간
  166. * @param string $value
  167. */
  168. 공개 정적 함수 checkTime($str){
  169. $timeArr =explore(":", $str)
  170. if (is_numeric($timeArr[0]) && is_numeric($timeArr[1]) && is_numeric ($ timeArr[2])) {
  171. if (($timeArr[0] >= 0 && $timeArr[0] <= 23) && ($timeArr[1] >= 0 && $timeArr[ 1] <= 59) && ($timeArr[2] >= 0 && $timeArr[2] <= 59))
  172. true를 반환합니다.
  173. else
  174. false를 반환합니다.
  175. false 반환
  176. }
  177. }
  178. 코드 복사

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.