PHP Common Regular expressions, verify user name, password, email,url, etc.

Source: Internet
Author: User
  1. Class verify{
  2. /**
  3. * Verify 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 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 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 your phone
  58. * @param string $value
  59. * @param string $match
  60. * @return Boolean
  61. */
  62. public static function IsMobile ($value, $match = '/^[(86) | 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 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 IP
  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] Little Shellfish [0-9]{2}| [1-9] Beckham [0-9] Little Shellfish | [1-9]) \. (25[0-5]|2[0-4][0-9]| [0-1] Little Shellfish [0-9]{2}| [1-9] Beckham [0-9] Little Shellfish | [1-9]|0] \. (25[0-5]|2[0-4][0-9]| [0-1] Little Shellfish [0-9]{2}| [1-9] Beckham [0-9] Little Shellfish | [1-9]|0] \. (25[0-5]|2[0-4][0-9]| [0-1] Little Shellfish [0-9]{2}| [1-9] Beckham [0-9] Little Shellfish | [0-9]) $/'){
  87. $v = Trim ($value);
  88. if (empty ($v))
  89. return false;
  90. Return Preg_match ($match, $v);
  91. }
  92. /**
  93. * Verify ID 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 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. ?>
Copy Code
Regular expressions, PHP, URLs
  • 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.