当前位置:首页 > PHP教程 > 正则表达式 > 列表

php表单习惯用的正则表达式

发布:smiling 来源: PHP粉丝网  添加日期:2021-08-12 10:24:53 浏览: 评论:0 

这篇文章通过实例代码给大家介绍了php表单习惯使用的正则表达式,非常不错,具有参考借鉴价值,需要的朋友参考下吧。

php表单常用正则表达式,代码如下所示:

  1. function is_email($str){  
  2. //检验email  
  3. return preg_match("/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/", $str);  
  4. }  
  5. function is_url($str){  
  6. //检验网址  
  7. return preg_match("/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"])  
  8. *$/", $str);  
  9. }  
  10. function is_qq($str){  
  11. //检验qq  
  12. return preg_match("/^[1-9]\d{4,8}$/", $str);  
  13. }  
  14. function is_zip($str){  
  15. //检验邮编  
  16. return preg_match("/^[1-9]\d{5}$/", $str);  
  17. }  
  18. function is_idcard($str){  
  19. //检验身份证  
  20. return preg_match("/^\d{15}(\d{2}[A-Za-z0-9])?$/", $str);  
  21. }  
  22. function is_chinese($str){  
  23. 检验是否是中文  
  24. return ereg("^[".chr(0xa1)."-".chr(0xff)."]+$",$str);  
  25. }  
  26. function is_english($str){  
  27. //检验是否是英文  
  28. return preg_match("/^[A-Za-z]+$/", $str);  
  29. }  
  30. function is_mobile($str){  
  31. //检验是否是手机  
  32. return preg_match("/^((\(\d{3}\))|(\d{3}\-))?13\d{9}$/", $str);  
  33. }  
  34. function is_phone($str){  
  35. //建云那是否是电话  
  36. return preg_match("/^((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}$/",  
  37. $str);  
  38. }  
  39. function is_safe($str){  
  40. return (preg_match("/^(([A-Z]*|[a-z]*|\d*|[-_\~!@#\$%\^&\*\.\(\)\[\]\{\}<>\?\\\/\'\"]*)|.  
  41. {0,5})$|\s/", $str) != 0);  
  42. }  
  43. } 

PS:下面再给大家分享一段代码

  1. <?php 
  2. /** 
  3. * @description: 正则表达式匹配 
  4. */ 
  5. class Regex { 
  6. /** 
  7. * @手机号 
  8. */ 
  9. public static function Phone($subject) { 
  10. $pattern='/^(0|86|17951)?(13[0-9]|15[012356789]|1[78][0-9]|14[57])[0-9]{8}$/'; 
  11. return Regex::PublicMethod($pattern, $subject); 
  12. } 
  13. /** 
  14. * @数字 
  15. */ 
  16. public static function Number($subject) { 
  17. $pattern='/^[0-9]+$/'; 
  18. return Regex::PublicMethod($pattern, $subject); 
  19. } 
  20. /** 
  21. * @年份 格式:yyyy 
  22. */ 
  23. public static function Year($subject) { 
  24. $pattern='/^(\d{4})$/'; 
  25. return Regex::PublicMethod($pattern, $subject); 
  26. } 
  27. /** 
  28. * @月份 格式:mm 
  29. */ 
  30. public static function Month($subject) { 
  31. $pattern='/^0?([1-9])$|^(1[0-2])$/'; 
  32. return Regex::PublicMethod($pattern, $subject); 
  33. } 
  34. /** 
  35. * @日期 格式:yyyy-mm-dd 
  36. */ 
  37. public static function Day($subject) { 
  38. $pattern='/^(\d{4})-(0?\d{1}|1[0-2])-(0?\d{1}|[12]\d{1}|3[01])$/'; 
  39. return Regex::PublicMethod($pattern, $subject); 
  40. } 
  41. /** 
  42. * @日期时间 格式:yyyy-mm-dd hh:ii:ss 
  43. */ 
  44. public static function DateTime($subject) { 
  45. $pattern='/^(\d{4})-(0?\d{1}|1[0-2])-(0?\d{1}|[12]\d{1}|3[01])\s(0\d{1}|1\d{1}|2[0-3]):[0-5]\d{1}:([0-5]\d{1})$/'; 
  46. return Regex::PublicMethod($pattern, $subject); 
  47. } 
  48. /** 
  49. * @邮箱 
  50. */ 
  51. public static function Email($subject) { 
  52. $pattern='/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/'; 
  53. return Regex::PublicMethod($pattern, $subject); 
  54. } 
  55. /** 
  56. * @邮编 
  57. */ 
  58. public static function Postcode($subject) { 
  59. $pattern='/[1-9]\d{5}(?!\d)/'; 
  60. return Regex::PublicMethod($pattern, $subject); 
  61. } 
  62. /** 
  63. * @有效图片地址 
  64. */ 
  65. public static function Photo($subject) { 
  66. $pattern='/\b(([\w-]+:\/\/?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|\/)))/'; 
  67. return Regex::PublicMethod($pattern, $subject); 
  68. } 
  69. /** 
  70. * @URL地址 
  71. */ 
  72. public static function UrlAddress($subject) { 
  73. $pattern='/\b(([\w-]+:\/\/?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|\/)))/'; 
  74. return Regex::PublicMethod($pattern, $subject); 
  75. } 
  76. /** 
  77. * @有效HTTP地址 
  78. */ 
  79. public static function EffectiveHttp($subject) { 
  80. $pattern='/\b(([\w-]+:\/\/?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|\/)))/'; 
  81. return Regex::PublicMethod($pattern, $subject); 
  82. } 
  83. /** 
  84. * @身份证 
  85. */ 
  86. public static function Identity($subject) { 
  87. $pattern='/(^\d{15}$)|(^\d{17}([0-9]|X)$)/'; 
  88. return Regex::PublicMethod($pattern, $subject); 
  89. } 
  90. /** 
  91. * @IPv4 
  92. */ 
  93. public static function Ipv4($subject) { 
  94. $pattern='/^(((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))\.){3}((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))$/'; 
  95. return Regex::PublicMethod($pattern, $subject); 
  96. } 
  97. /** 
  98. * @IPv6 
  99. */ 
  100. public static function Ipv6($subject) { 
  101. $pattern='/^([\da-fA-F]{1,4}:){7}[\da-fA-F]{1,4}$/'; 
  102. return Regex::PublicMethod($pattern, $subject); 
  103. } 
  104. /** 
  105. * @匹配正则公共方法 
  106. */ 
  107. public static function PublicMethod($pattern, $subject){ 
  108. if(preg_match($pattern, $subject)){ 
  109. return true; 
  110. } 
  111. return false; 
  112. } 
  113. }

Tags: php正则表达式

分享到: