You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

363 lines
11 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Common;
  3. class Helper
  4. {
  5. //保留两位小数,最后一位会四舍五入
  6. public static function formatPrice($price)
  7. {
  8. return sprintf("%.2f",$price);
  9. }
  10. //验证是否是合法的手机号码
  11. public static function isValidMobile($mobile)
  12. {
  13. return preg_match('/^(13[0-9]|14[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$/', $mobile);
  14. }
  15. //验证是否是合法中文
  16. public static function isValidChinese($word, $length = 16)
  17. {
  18. $pattern = "/(^[\x{4e00}-\x{9fa5}]+)/u";
  19. preg_match($pattern, $word, $match);
  20. if (!$match)
  21. {
  22. return false;
  23. }
  24. if (mb_strlen($match[1]) > $length)
  25. {
  26. return false;
  27. }
  28. return $match[1];
  29. }
  30. //验证是否是合法的身份证号,简单验证
  31. public static function isValidIdCardNo($idcard)
  32. {
  33. $length = strlen($idcard);
  34. //15位老身份证
  35. if ($length == 15)
  36. {
  37. if (checkdate(substr($idcard, 8, 2), substr($idcard, 10, 2), '19' . substr($idcard, 6, 2)))
  38. {
  39. return true;
  40. }
  41. }
  42. //18位二代身份证号
  43. if ($length == 18)
  44. {
  45. if (!checkdate(substr($idcard, 10, 2), substr($idcard, 12, 2), substr($idcard, 6, 4)))
  46. {
  47. return false;
  48. }
  49. $idcard = str_split($idcard);
  50. if (strtolower($idcard[17]) == 'x')
  51. {
  52. $idcard[17] = '10';
  53. }
  54. //加权求和
  55. $sum = 0;
  56. //加权因子
  57. $wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1];
  58. for ($i = 0; $i < 17; $i++)
  59. {
  60. $sum += $wi[$i] * $idcard[$i];
  61. }
  62. //得到验证码所位置
  63. $position = $sum % 11;
  64. //身份证验证位值 10代表X
  65. $code = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2];
  66. if ($idcard[17] == $code[$position])
  67. {
  68. return true;
  69. }
  70. }
  71. return false;
  72. }
  73. //验证是否是合法的银行卡,不包含信用卡
  74. public static function isValidBankCard($card)
  75. {
  76. if (!is_numeric($card))
  77. {
  78. return false;
  79. }
  80. if (strlen($card) < 16 || strlen($card) > 19)
  81. {
  82. return false;
  83. }
  84. $cardHeader = [10, 18, 30, 35, 37, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 58, 60, 62, 65, 68, 69, 84, 87, 88, 94, 95, 98, 99];
  85. if (!in_array(substr($card, 0, 2), $cardHeader))
  86. {
  87. return false;
  88. }
  89. $numShouldCheck = str_split(substr($card, 0, -1));
  90. krsort($numShouldCheck);
  91. $odd = $odd['gt9'] = $odd['gt9']['tens'] = $odd['gt9']['unit'] = $odd['lt9'] = $even = [];
  92. array_walk($numShouldCheck, function ($item, $key) use (&$odd, &$even, $card){
  93. if ((strlen($card) == 16) && (substr($card, 0, 2) == '62'))
  94. {
  95. $key += 1;
  96. }
  97. if (($key & 1))
  98. {
  99. $t = $item * 2;
  100. if ($t > 9)
  101. {
  102. $odd['gt9']['unit'][] = intval($t % 10);
  103. $odd['gt9']['tens'][] = intval($t / 10);
  104. }
  105. else
  106. {
  107. $odd['lt9'][] = $t;
  108. }
  109. }
  110. else
  111. {
  112. $even[] = $item;
  113. }
  114. });
  115. $total = array_sum($even);
  116. array_walk_recursive($odd, function ($item, $key) use (&$total) {
  117. $total += $item;
  118. });
  119. $luhm = 10 - ($total % 10 == 0 ? 10 : $total % 10);
  120. $lastNumOfCard = substr($card, -1, 1);
  121. if ($luhm != $lastNumOfCard)
  122. {
  123. return false;
  124. }
  125. return true;
  126. }
  127. //随机字母
  128. public static function randLetter($len)
  129. {
  130. $letter = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
  131. $result = '';
  132. for ($i = 0; $i < $len; $i++)
  133. {
  134. $result .= $letter[array_rand($letter, 1)];
  135. }
  136. return $result;
  137. }
  138. //生成二维码
  139. public static function qrcode($url,$size=150)
  140. {
  141. return 'data:image/png;base64,'.base64_encode(\QrCode::format('png')->encoding('UTF-8')->size($size)->margin(0)->errorCorrection('H')->generate($url));
  142. }
  143. //获取浏览器信息
  144. public static function getBrowser()
  145. {
  146. $browser = array('name'=>'unknown', 'version'=>'unknown');
  147. if(empty($_SERVER['HTTP_USER_AGENT'])) return $browser;
  148. $agent = $_SERVER["HTTP_USER_AGENT"];
  149. // Chrome should checked before safari
  150. if(strpos($agent, 'Firefox') !== false) $browser['name'] = "firefox";
  151. if(strpos($agent, 'Opera') !== false) $browser['name'] = 'opera';
  152. if(strpos($agent, 'Safari') !== false) $browser['name'] = 'safari';
  153. if(strpos($agent, 'Chrome') !== false) $browser['name'] = "chrome";
  154. // Check the name of browser
  155. if(strpos($agent, 'MSIE') !== false || strpos($agent, 'rv:11.0')) $browser['name'] = 'ie';
  156. if(strpos($agent, 'Edge') !== false) $browser['name'] = 'edge';
  157. // Check the version of browser
  158. if(preg_match('/MSIE\s(\d+)\..*/i', $agent, $regs)) $browser['version'] = $regs[1];
  159. if(preg_match('/FireFox\/(\d+)\..*/i', $agent, $regs)) $browser['version'] = $regs[1];
  160. if(preg_match('/Opera[\s|\/](\d+)\..*/i', $agent, $regs)) $browser['version'] = $regs[1];
  161. if(preg_match('/Chrome\/(\d+)\..*/i', $agent, $regs)) $browser['version'] = $regs[1];
  162. if((strpos($agent, 'Chrome') == false) && preg_match('/Safari\/(\d+)\..*$/i', $agent, $regs)) $browser['version'] = $regs[1];
  163. if(preg_match('/rv:(\d+)\..*/i', $agent, $regs)) $browser['version'] = $regs[1];
  164. if(preg_match('/Edge\/(\d+)\..*/i', $agent, $regs)) $browser['version'] = $regs[1];
  165. return $browser;
  166. }
  167. /**
  168. * 检查是否是AJAX请求。
  169. * Check is ajax request.
  170. *
  171. * @static
  172. * @access public
  173. * @return bool
  174. */
  175. public static function isAjaxRequest()
  176. {
  177. if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') return true;
  178. if(isset($_GET['HTTP_X_REQUESTED_WITH']) && $_GET['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') return true;
  179. return false;
  180. }
  181. /**
  182. * 检查是否是POST请求
  183. */
  184. public static function isPostRequest()
  185. {
  186. if($_SERVER['REQUEST_METHOD'] == 'POST') return true;
  187. if($_POST) return true;
  188. return false;
  189. }
  190. /**
  191. * 是否是GET提交的
  192. */
  193. public static function isGetRequest()
  194. {
  195. return $_SERVER['REQUEST_METHOD'] == 'GET' ? true : false;
  196. }
  197. /**
  198. * 301跳转。
  199. * Header 301 Moved Permanently.
  200. *
  201. * @param string $locate
  202. * @access public
  203. * @return void
  204. */
  205. public static function header301($locate)
  206. {
  207. header('HTTP/1.1 301 Moved Permanently');
  208. die(header('Location:' . $locate));
  209. }
  210. /**
  211. * 获取远程IP。
  212. * Get remote ip.
  213. *
  214. * @access public
  215. * @return string
  216. */
  217. public static function getRemoteIp()
  218. {
  219. $ip = '';
  220. if(!empty($_SERVER["REMOTE_ADDR"])) $ip = $_SERVER["REMOTE_ADDR"];
  221. if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
  222. if(!empty($_SERVER['HTTP_CLIENT_IP'])) $ip = $_SERVER['HTTP_CLIENT_IP'];
  223. return $ip;
  224. }
  225. /**
  226. * 建立文件夹
  227. *
  228. * @param string $aimUrl
  229. * @return viod
  230. */
  231. public static function createDir($aimUrl)
  232. {
  233. $aimUrl = str_replace('', '/', $aimUrl);
  234. $aimDir = '';
  235. $arr = explode('/', $aimUrl);
  236. $result = true;
  237. foreach ($arr as $str)
  238. {
  239. $aimDir .= $str . '/';
  240. if (!file_exists($aimDir))
  241. {
  242. $result = mkdir($aimDir);
  243. }
  244. }
  245. return $result;
  246. }
  247. //判断访问终端是否是微信浏览器
  248. public static function isWechatBrowser()
  249. {
  250. if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false)
  251. {
  252. return true;
  253. }
  254. return false;
  255. }
  256. //判断是不是https
  257. public static function isHttpsRequest()
  258. {
  259. if((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) return true;
  260. if($_SERVER['SERVER_PORT'] == 443) return true;
  261. return false;
  262. }
  263. /**
  264. * @name php获取中文字符拼音首字母
  265. * @param $str
  266. * @return null|string
  267. */
  268. public function getFirstCharter($str)
  269. {
  270. if (empty($str))
  271. {
  272. return '';
  273. }
  274. $fchar = ord($str{0});
  275. if ($fchar >= ord('A') && $fchar <= ord('z')) return strtoupper($str{0});
  276. $s1 = iconv('UTF-8', 'gb2312', $str);
  277. $s2 = iconv('gb2312', 'UTF-8', $s1);
  278. $s = $s2 == $str ? $s1 : $str;
  279. $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
  280. if ($asc >= -20319 && $asc <= -20284) return 'A';
  281. if ($asc >= -20283 && $asc <= -19776) return 'B';
  282. if ($asc >= -19775 && $asc <= -19219) return 'C';
  283. if ($asc >= -19218 && $asc <= -18711) return 'D';
  284. if ($asc >= -18710 && $asc <= -18527) return 'E';
  285. if ($asc >= -18526 && $asc <= -18240) return 'F';
  286. if ($asc >= -18239 && $asc <= -17923) return 'G';
  287. if ($asc >= -17922 && $asc <= -17418) return 'H';
  288. if ($asc >= -17417 && $asc <= -16475) return 'J';
  289. if ($asc >= -16474 && $asc <= -16213) return 'K';
  290. if ($asc >= -16212 && $asc <= -15641) return 'L';
  291. if ($asc >= -15640 && $asc <= -15166) return 'M';
  292. if ($asc >= -15165 && $asc <= -14923) return 'N';
  293. if ($asc >= -14922 && $asc <= -14915) return 'O';
  294. if ($asc >= -14914 && $asc <= -14631) return 'P';
  295. if ($asc >= -14630 && $asc <= -14150) return 'Q';
  296. if ($asc >= -14149 && $asc <= -14091) return 'R';
  297. if ($asc >= -14090 && $asc <= -13319) return 'S';
  298. if ($asc >= -13318 && $asc <= -12839) return 'T';
  299. if ($asc >= -12838 && $asc <= -12557) return 'W';
  300. if ($asc >= -12556 && $asc <= -11848) return 'X';
  301. if ($asc >= -11847 && $asc <= -11056) return 'Y';
  302. if ($asc >= -11055 && $asc <= -10247) return 'Z';
  303. return '';
  304. }
  305. }