Check.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. //执行验证规则
  3. /*
  4. 用法:
  5. Check::rule(
  6. array(验证函数1,'错误返回值1'),
  7. array(验证函数2,'错误返回值2'),
  8. );
  9. 若有一个验证函数返回false,则返回对应的错误返回值,若全部通过验证,则返回true。
  10. 验证函数,可以是自定义的函数或类方法,返回true表示通过,返回false,表示没有通过
  11. */
  12. namespace App\Repositories\Utils;
  13. class Check
  14. {
  15. public static function rule($array = array())
  16. {
  17. if (!isset($array[0][0])) {
  18. $array = func_get_args();
  19. }
  20. if (is_array($array)) {
  21. foreach ($array as $vo) {
  22. if (is_array($vo)&&isset($vo[0])&&isset($vo[1])) {
  23. if (!$vo[0]) {
  24. return $vo[1];
  25. }
  26. }
  27. }
  28. }
  29. return true;
  30. }
  31. /**
  32. * 检查字符串长度
  33. *
  34. * @access public
  35. * @param string $str 输入字符串
  36. * @param int $min 最小长度
  37. * @param int $max 最大长度
  38. * @return bool
  39. */
  40. public static function len($str, $min = 0, $max = 255)
  41. {
  42. $str = trim($str);
  43. if (empty($str)) {
  44. return true;
  45. }
  46. $len = dstrlen($str);
  47. if (($len>=$min)&&($len<=$max)) {
  48. return true;
  49. } else {
  50. return false;
  51. }
  52. }
  53. /**
  54. * 检查字符串是否为空
  55. *
  56. * @access public
  57. * @param string $str 输入字符串
  58. * @return bool
  59. */
  60. public static function must($str)
  61. {
  62. $str = trim($str);
  63. return !empty($str);
  64. }
  65. /**
  66. * 检查两次输入的值是否相同
  67. *
  68. * @access public
  69. * @param string $str1 输入字符串1
  70. * @param string $str2 输入字符串2
  71. * @return bool
  72. */
  73. public static function same($str1, $str2)
  74. {
  75. return $str1 == $str2;
  76. }
  77. /**
  78. * 检查用户名
  79. *
  80. * @access public
  81. * @param string $str 输入字符串
  82. * @param integer $len_min 最小长度
  83. * @param integer $len_max 最大长度
  84. * @param string $type 判断类型
  85. * @return bool
  86. */
  87. public static function userName($str, $len_min = 0, $len_max = 255, $type = 'ALL')
  88. {
  89. if (empty($str)) {
  90. return true;
  91. }
  92. if (self::len($str, $len_min, $len_max) == false) {
  93. return false;
  94. }
  95. switch ($type) {
  96. //纯英文
  97. case "EN":
  98. $pattern = "/^[a-zA-Z]+$/";
  99. break;
  100. //英文数字
  101. case "ENNUM":
  102. $pattern = "/^[a-zA-Z0-9]+$/";
  103. break;
  104. //by ninja911 包含中文,至少一个汉字、数字、字母、下划线(不能以_开头和结尾)
  105. case "CHINESE":
  106. $pattern = "/^(?!_)(?!.*?_$)[a-zA-Z0-9_\x{4e00}-\x{9fc3}]+$/u";
  107. break;
  108. //允许的符号(|-_字母数字)
  109. case "CHINESE2":
  110. $pattern = "/^[a-zA-Z\x{4e00}-\x{9fc3}][a-zA-Z0-9_\x{4e00}-\x{9fc3}]+$/u";
  111. break;
  112. //允许的符号(|-_字母数字)
  113. case "ALL":
  114. $pattern = "/^[\-\_a-zA-Z0-9]+$/";
  115. break;
  116. //用户自定义正则
  117. default:
  118. $pattern = $type;
  119. break;
  120. }
  121. if (preg_match($pattern, $str)) {
  122. return true;
  123. } else {
  124. return false;
  125. }
  126. }
  127. /**
  128. * 验证邮箱
  129. *
  130. * @access public
  131. * @param string $str 输入字符串
  132. * @return bool
  133. */
  134. public static function email($str)
  135. {
  136. if (empty($str)) {
  137. return true;
  138. }
  139. $chars = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,6}\$/i";
  140. if (strpos($str, '@') !== false && strpos($str, '.') !== false) {
  141. if (preg_match($chars, $str)) {
  142. return true;
  143. } else {
  144. return false;
  145. }
  146. } else {
  147. return false;
  148. }
  149. }
  150. /**
  151. * 验证手机号码
  152. *
  153. * @access public
  154. * @param string $str 输入字符串
  155. * @return bool
  156. */
  157. public static function mobile($str)
  158. {
  159. if (empty($str)) {
  160. return true;
  161. }
  162. return preg_match('#^13[\d]{9}$|14^[0-9]\d{8}|^15[0-9]\d{8}$|^18[0-9]\d{8}$|^17[0-9]\d{8}$#', $str);
  163. }
  164. /**
  165. * 验证固定电话
  166. *
  167. * @access public
  168. * @param string $str 输入字符串
  169. * @return bool
  170. */
  171. public static function tel($str)
  172. {
  173. if (empty($str)) {
  174. return true;
  175. }
  176. return preg_match('/^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/', trim($str));
  177. }
  178. /**
  179. * 验证qq号码
  180. *
  181. * @access public
  182. * @param string $str 输入字符串
  183. * @return bool
  184. */
  185. public static function qq($str)
  186. {
  187. if (empty($str)) {
  188. return true;
  189. }
  190. return preg_match('/^[1-9]\d{4,12}$/', trim($str));
  191. }
  192. /**
  193. * 验证邮政编码
  194. *
  195. * @access public
  196. * @param string $str 输入字符串
  197. * @return bool
  198. */
  199. public static function zipCode($str)
  200. {
  201. if (empty($str)) {
  202. return true;
  203. }
  204. return preg_match('/^[1-9]\d{5}$/', trim($str));
  205. }
  206. /**
  207. * 验证ip
  208. *
  209. * @access public
  210. * @param string $str 输入字符串
  211. * @return bool
  212. */
  213. public static function ip($str)
  214. {
  215. if (empty($str)) {
  216. return true;
  217. }
  218. if (!preg_match('#^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$#', $str)) {
  219. return false;
  220. }
  221. $ip_array = explode('.', $str);
  222. //真实的ip地址每个数字不能大于255(0-255)
  223. return ($ip_array[0]<=255 && $ip_array[1]<=255 && $ip_array[2]<=255 && $ip_array[3]<=255) ?
  224. true : false;
  225. }
  226. /**
  227. * 验证身份证(中国)
  228. *
  229. * @access public
  230. * @param string $str 输入字符串
  231. * @return bool
  232. */
  233. public static function idCard($str)
  234. {
  235. $str = trim($str);
  236. if (empty($str)) {
  237. return true;
  238. }
  239. if (preg_match("/^([0-9]{15}|[0-9]{17}[0-9a-z])$/i", $str)) {
  240. return true;
  241. } else {
  242. return false;
  243. }
  244. }
  245. /**
  246. * 验证网址
  247. *
  248. * @access public
  249. * @param string $str 输入字符串
  250. * @return bool
  251. */
  252. public static function url($str)
  253. {
  254. if (empty($str)) {
  255. return true;
  256. }
  257. return preg_match(
  258. "/^(http|https|ftp|ftps):\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/",
  259. $str
  260. ) ? true : false;
  261. }
  262. /**
  263. * 判断目录名是否合法
  264. *
  265. * @access public
  266. * @param string $str 输入字符串
  267. * @return bool
  268. */
  269. public static function dirName($str)
  270. {
  271. $str = !empty($str) ? trim($str) : '';
  272. if (empty($str)) {
  273. return false;
  274. }
  275. return preg_match('/^(?!\\/)[\-\_\\/a-zA-Z0-9]+$/', $str);
  276. }
  277. /**
  278. * 判断是否为指定长度内字符串
  279. *
  280. * @access public
  281. * @param string $str 输入字符串
  282. * @param integer $min 最小长度
  283. * @param integer $max 最大长度
  284. * @return bool
  285. */
  286. public static function lengthBetween($str, $min = 0, $max = 0)
  287. {
  288. $str_length = $str ? dstrlen($str) : 0;
  289. if ($min > 0 && $max > 0) {
  290. if (!($str_length >= $min && $str_length <= $max)) {
  291. return false;
  292. } else {
  293. return true;
  294. }
  295. } elseif ($min > 0 && $max == 0) {
  296. if ($str_length < $min) {
  297. return false;
  298. } else {
  299. return true;
  300. }
  301. } elseif ($max > 0 && $min == 0) {
  302. if ($str_length > $max) {
  303. return false;
  304. } else {
  305. return true;
  306. }
  307. } else {
  308. return true;
  309. }
  310. }
  311. /**
  312. * 判断文件扩展名合法
  313. *
  314. * @access public
  315. * @param string $file_name 文件名
  316. * @param array $allow_ext 允许扩展名数组
  317. * @return bool
  318. */
  319. public static function fileAllowExt($file_name, $allow_ext = array())
  320. {
  321. $file_ext = file_ext($file_name);
  322. if ($file_ext) {
  323. if ($allow_ext) {
  324. if (!in_array($file_ext, $allow_ext)) {
  325. return false;
  326. } else {
  327. return true;
  328. }
  329. } else {
  330. return true;
  331. }
  332. } else {
  333. return false;
  334. }
  335. }
  336. }