paramcheck.class.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * 参数检查类
  4. *
  5. * 收集常用的检查参数的函数集中于此类
  6. *
  7. * @createtime 2018/03/01
  8. * @author 空竹
  9. * @copyright 芝麻开发(http://www.zhimawork.com)
  10. */
  11. class ParamCheck {
  12. /**
  13. * is_mobile() 手机号格式是否正确
  14. *
  15. */
  16. static public function is_mobile($mobile){
  17. if(preg_match("/^1[0-9]{10}$/", $mobile)){
  18. return true;
  19. }else{
  20. return false;
  21. }
  22. }
  23. /**
  24. * is_email()邮箱格式是否正确
  25. * 2016/7/27更新正则表达式
  26. */
  27. static public function is_email($email)
  28. {
  29. if(preg_match("/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z]{2,}$/", $email)){
  30. return true;
  31. }else{
  32. return false;
  33. }
  34. }
  35. /**
  36. * 检查时间格式
  37. * 形如:yyyy-mm-dd hh:ii:ss
  38. *
  39. */
  40. static public function is_datetime($timestr){
  41. $patten = "/^([0-9]{4})-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])\s+(0?[0-9]|1[0-9]|2[0-3]):(0?[0-9]|[1-5][0-9]):(0?[0-9]|[1-5][0-9])$/";
  42. if(preg_match($patten, $timestr, $parts)){
  43. if(checkdate($parts[2], $parts[3], $parts[1]))
  44. return true;
  45. else
  46. return false;
  47. }else{
  48. return false;
  49. }
  50. }
  51. /**
  52. * 检查日期格式
  53. * 形如:yyyy-mm-dd
  54. *
  55. */
  56. static public function is_date($datestr){
  57. $patten = "/^([0-9]{4})-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])$/";
  58. if(preg_match($patten, $datestr, $parts)){
  59. if(checkdate($parts[2], $parts[3], $parts[1]))
  60. return true;
  61. else
  62. return false;
  63. }else{
  64. return false;
  65. }
  66. }
  67. /**
  68. * 检查弱密码
  69. * @return 弱密码--true 非弱密码--false
  70. */
  71. static public function is_weakPwd($pwd){
  72. if(strlen($pwd)<6) return true;
  73. $weakPwd = array(
  74. '000000','0000000',
  75. '111111','666666','888888','999999','88888888','11111111',
  76. 'aaaaaa',
  77. '123456789','1234567890','0123456789','12345678',
  78. '123456','654321','abcdef',
  79. 'abc123','123123','321321','112233','abcabc',
  80. 'aaa111','a1b2c3',
  81. 'qwerty','qweasd',
  82. 'password','p@ssword','passwd','passw0rd',
  83. 'iloveyou','5201314',
  84. 'admin1234','admin888','admin123'
  85. );
  86. if(in_array($pwd, $weakPwd))
  87. return true;
  88. else
  89. return false;
  90. }
  91. /**
  92. * 检查是否为数字(包括整数、小数和负数)
  93. * @param string
  94. * @return boolean
  95. */
  96. static public function is_number($val){
  97. return (bool)preg_match('/^\-?([0-9]{1}|[1-9]+[0-9]*)(\.[0-9]+)?$/', $val);
  98. }
  99. /**
  100. * 检查是否为正整数,多用于检查ID合法性
  101. * @param string
  102. * @return boolean
  103. */
  104. static public function is_ID($val){
  105. return (bool)preg_match('/^([1-9]+[0-9]*)$/', $val);
  106. }
  107. /**
  108. * 检查是否为身份证号
  109. * @param string
  110. * @return boolean
  111. */
  112. static public function is_idcard($val){
  113. return (bool)preg_match('/(^\d{15}$)|(^\d{17}(\d|X)$)/', $val);
  114. }
  115. /**
  116. * 检查是否为邮政编码
  117. * @param string
  118. * @return boolean
  119. */
  120. static public function is_postcode($val){
  121. return (bool)preg_match('/^(\d{6})$/', $val);
  122. }
  123. }
  124. ?>