Home  >  Article  >  Backend Development  >  Commonly used regular expressions in PHP to verify username, password, Email, Url, etc.

Commonly used regular expressions in PHP to verify username, password, Email, Url, etc.

WBOY
WBOYOriginal
2016-07-25 08:45:072139browse
  1. class Verify{
  2. /**
  3. * Verify username
  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 = '/^[_wd]{'.$minLen.','.$maxLen.'}$/iu';
  13. break;
  14. case 'CN':$match = '/^[_x{4e00}-x{9fa5}d]{'.$minLen.','.$maxLen.'}$/iu';
  15. break;
  16. default:$match = '/^[_wdx{4e00}-x{9fa5}]{'.$minLen.','.$maxLen.'}$/iu';
  17. }
  18. return preg_match($match,$value);
  19. }
  20. /**
  21. * Verification 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='/^[\~!@#$%^&*()-_=+|{}[],.?/:;'"dw]{'.$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='/^[wd]+[wd-.]*@[wd-.]+.[wd]{2,10}$/i'){
  40. $v = trim($value);
  41. if(empty($v))
  42. return false;
  43. return preg_match($match,$v);
  44. }
  45. /**
  46. * Verification 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
  58. * @param string $value
  59. * @param string $match
  60. * @return boolean
  61. */
  62. public static function isMobile($value,$match='/^[(86)|0]?(13d{9})|(15d{9})|(18d{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]小贝[0-9]{2}|[1-9]小贝[0-9]小贝|[1-9]).(25[0-5]|2[0-4][0-9]|[0-1]小贝[0-9]{2}|[1-9]小贝[0-9]小贝|[1-9]|0).(25[0-5]|2[0-4][0-9]|[0-1]小贝[0-9]{2}|[1-9]小贝[0-9]小贝|[1-9]|0).(25[0-5]|2[0-4][0-9]|[0-1]小贝[0-9]{2}|[1-9]小贝[0-9]小贝|[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])|(2d))d{2}((0d)|(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. * Verification URL
  109. * @param string $value
  110. * @param string $match
  111. * @return boolean
  112. */
  113. public static function isURL($value,$match='/^(http://)?(https://)?([wd-]+.)+[w-]+(/[dw-./?%&=]*)?$/'){
  114. $v = strtolower(trim($value));
  115. if(empty($v))
  116. return false;
  117. return preg_match($match,$v);
  118. }
  119. }
  120. ?>
复制代码

正则表达式, php, Url


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn