Common php regular expressions, authentication username, password, Email, Url, etc.

Source: Internet
Author: User
Common php regular expressions, authentication username, password, Email, Url, etc.

  1. Class Verify {
  2. /**
  3. * Verify the user name
  4. * @ Param string $ value
  5. * @ Param int $ length
  6. * @ Return boolean
  7. */
  8. Public static function isNames ($ value, $ minLen = 2, $ maxLen = 20, $ charset = 'all '){
  9. If (empty ($ value ))
  10. Return false;
  11. Switch ($ charset ){
  12. Case 'en': $ match = '/^ [_ \ w \ d] {'. $ minLen. ','. $ maxLen. '} $/iu ';
  13. Break;
  14. Case 'cn': $ match = '/^ [_ \ x {4e00}-\ x {9fa5} \ d] {'. $ minLen. ','. $ maxLen. '} $/iu ';
  15. Break;
  16. Default: $ match = '/^ [_ \ w \ d \ x {4e00}-\ x {9fa5}] {'. $ minLen. ','. $ maxLen. '} $/iu ';
  17. }
  18. Return preg_match ($ match, $ value );
  19. }
  20. /**
  21. * Verify the password
  22. * @ Param string $ value
  23. * @ Param int $ length
  24. * @ Return boolean
  25. */
  26. Public static function isPWD ($ value, $ minLen = 5, $ maxLen = 16 ){
  27. $ Match = '/^ [\\~! @ # $ % ^ & * ()-_ = + | {}\ [\],.? \/:; \ '\ "\ D \ w] {'. $ minLen. ','. $ maxLen. '} $ /';
  28. $ V = trim ($ value );
  29. If (empty ($ v ))
  30. Return false;
  31. Return preg_match ($ match, $ v );
  32. }
  33. /**
  34. * Verify eamil
  35. * @ Param string $ value
  36. * @ Param int $ length
  37. * @ Return boolean
  38. */
  39. Public static function isEmail ($ value, $ match = '/^ [\ w \ d] + [\ w \ d-.] * @ [\ w \ d-.] + \. [\ w \ d] {2, 10} $/I '){
  40. $ V = trim ($ value );
  41. If (empty ($ v ))
  42. Return false;
  43. Return preg_match ($ match, $ v );
  44. }
  45. /**
  46. * Verify the phone number
  47. * @ Param string $ value
  48. * @ Return boolean
  49. */
  50. Public static function isTelephone ($ value, $ match = '/^ 0 [0-9] {2, 3} [-]? \ D {7, 8} $ /'){
  51. $ V = trim ($ value );
  52. If (empty ($ v ))
  53. Return false;
  54. Return preg_match ($ match, $ v );
  55. }
  56. /**
  57. * Verify mobile phone number
  58. * @ Param string $ value
  59. * @ Param string $ match
  60. * @ Return boolean
  61. */
  62. Public static function isMobile ($ value, $ match = '/^ [(86) | 0]? (13 \ d {9}) | (15 \ d {9}) | (18 \ d {9}) $ /'){
  63. $ V = trim ($ value );
  64. If (empty ($ v ))
  65. Return false;
  66. Return preg_match ($ match, $ v );
  67. }
  68. /**
  69. * Verify the ZIP code
  70. * @ Param string $ value
  71. * @ Param string $ match
  72. * @ Return boolean
  73. */
  74. Public static function isPostcode ($ value, $ match = '/\ d {6 }/'){
  75. $ V = trim ($ value );
  76. If (empty ($ v ))
  77. Return false;
  78. Return preg_match ($ match, $ v );
  79. }
  80. /**
  81. * Verify the IP address
  82. * @ Param string $ value
  83. * @ Param string $ match
  84. * @ Return boolean
  85. */
  86. Public static function isIP ($ value, $ match = '/^ (25 [0-5] | 2 [0-4] [0-9] | [0-1] Beckham [0-9] {2} | [1-9] Beckham [0-9] Beckham | [1-9]) \. (25 [0-5] | 2 [0-4] [0-9] | [0-1] Beckham [0-9] {2} | [1-9] beckham [0-9] Beckham | [1-9] | 0) \. (25 [0-5] | 2 [0-4] [0-9] | [0-1] Beckham [0-9] {2} | [1-9] beckham [0-9] Beckham | [1-9] | 0) \. (25 [0-5] | 2 [0-4] [0-9] | [0-1] Beckham [0-9] {2} | [1-9] beckham [0-9] Beckham | [0-9]) $ /'){
  87. $ V = trim ($ value );
  88. If (empty ($ v ))
  89. Return false;
  90. Return preg_match ($ match, $ v );
  91. }
  92. /**
  93. * Verify the ID card number
  94. * @ Param string $ value
  95. * @ Param string $ match
  96. * @ Return boolean
  97. */
  98. Public static function isIDcard ($ value, $ match = '/^ \ d {6} (1 [89]) | (2 \ d )) \ d {2} (0 \ d) | (1 [0-2]) (3 [01]) | ([0-2] \ d )) \ d {3} (\ d | X) $/I '){
  99. $ V = trim ($ value );
  100. If (empty ($ v ))
  101. Return false;
  102. Else if (strlen ($ v)> 18)
  103. Return false;
  104. Return preg_match ($ match, $ v );
  105. }
  106. /**
  107. **
  108. * Verify the URL
  109. * @ Param string $ value
  110. * @ Param string $ match
  111. * @ Return boolean
  112. */
  113. Public static function isURL ($ value, $ match = '/^ (http :\/\/)? (Https :\/\/)? ([\ W \ d-] + \.) + [\ w-] + (\/[\ d \ w -.\/? % & =] *)? $ /'){
  114. $ V = strtolower (trim ($ value ));
  115. If (empty ($ v ))
  116. Return false;
  117. Return preg_match ($ match, $ v );
  118. }
  119. }
  120. ?>


Regular expression, php, Url

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.