当前位置:Gxlcms > PHP教程 > php常用正则表达式

php常用正则表达式

时间:2021-07-01 10:21:17 帮助过:4人阅读

  1. class Verify{
  2. /**
  3. * 验证用户名
  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. * 验证密码
  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. * 验证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. * 验证电话号码
  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. * 验证手机
  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. * 验证邮政编码
  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. * 验证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. * 验证身份证号码
  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. * 验证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. ?>

正则表达式, php

人气教程排行