A good PHP verification class-php Tutorial

Source: Internet
Author: User
A good PHP verification class

  1. /**
  2. * Verification class
  3. *
  4. * @ Lastmodify 5-5-16
  5. * @ Author jy625
  6. */
  7. Class VerifyAction {
  8. /**
  9. * Whether it is null
  10. */
  11. Public static function isEmpty ($ str ){
  12. $ Str = trim ($ str );
  13. Return! Empty ($ str )? True: false;
  14. }
  15. /**
  16. * Digit verification
  17. * Param: $ flag: whether the int is an integer or the float is a floating point.
  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 and directory name
  29. * @ Param: string $ str string to be matched
  30. * @ Param: $ whether chinese is supported. it is supported by default. if the file name is matched, disable this option (false)
  31. * @ Param: $ charset encoding (UTF-8 by default, 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. // Mobile phone number verification
  50. Public static function isMobile ($ str ){
  51. $ Exp = "/^ 13 [0-9] Beckham [0-9] {8} $ | 15 [012356789] Beckham [0-9] {8} $ | 18 [012356789] Beckham [0-9] {8 }$ | 14 [57] Beckham [0-9] $ /";
  52. If (preg_match ($ exp, $ str )){
  53. Return true;
  54. } Else {
  55. Return false;
  56. }
  57. }
  58. /**
  59. * URL verification, in pure URL format, does not support IP Address 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 characters
  67. * @ Param: string $ str string to be matched
  68. * @ Param: $ charset encoding (UTF-8 by default, supports 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 verification
  78. */
  79. Public static function isUtf8 ($ str ){
  80. If (! Self: isEmpty ($ str) return false;
  81. Return (preg_match ("/^ ([". chr (1, 228 ). "-". chr (1, 233 ). "] Beckham [". chr (1, 128 ). "-". chr (1, 191 ). "] Beckham [". chr (1, 128 ). "-". chr (1, 191 ). "] Beckham) Beckham/", $ word)
  82. = True | preg_match ("/([". chr (1, 228 ). "-". chr (1, 233 ). "] Beckham [". chr (1, 128 ). "-". chr (1, 191 ). "] Beckham [". chr (1, 128 ). "-". chr (1, 191 ). "] Beckham) Beckham $/", $ word)
  83. = True | preg_match ("/([". chr (1, 228 ). "-". chr (1, 233 ). "] Beckham [". chr (1, 128 ). "-". chr (1, 191 ). "] Beckham [". chr (1, 128 ). "-". chr (1, 191 ). "] Beckham) {2,}/", $ word)
  84. = True )? True: false;
  85. }
  86. /**
  87. * Verification length
  88. * @ Param: string $ str
  89. * @ Param: int $ type (method, default value: 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: // only match the minimum value
  98. Return ($ len >=$ min )? True: false;
  99. Break;
  100. Case 2: // only match the maximum value
  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. * Verify the 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. ,.? \/:; \ '\ "\ D \ w] {'. $ minLen. ','. $ maxLen. '} $ /';
  116. $ V = trim ($ value );
  117. If (empty ($ v ))
  118. Return false;
  119. Return preg_match ($ match, $ v );
  120. }
  121. /**
  122. * Verify the user name
  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. * Verify 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. * Matching 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. }


Good, PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.