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.

469 lines
15 KiB

7 years ago
7 years ago
6 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. * 取得随机字符串
  140. *
  141. * @param int $length 生成随机数的长度
  142. * @param int $numeric 是否只产生数字随机数 1是0否
  143. * @return string
  144. */
  145. public static function getRandomString($length, $numeric = 0)
  146. {
  147. $seed = base_convert(md5(microtime().$_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
  148. $seed = $numeric ? (str_replace('0', '', $seed).'012340567890') : ($seed.'zZ'.strtoupper($seed));
  149. $hash = '';
  150. $max = strlen($seed) - 1;
  151. for($i = 0; $i < $length; $i++)
  152. {
  153. $hash .= $seed{mt_rand(0, $max)};
  154. }
  155. return $hash;
  156. }
  157. //生成二维码
  158. public static function qrcode($url,$size=150)
  159. {
  160. return 'data:image/png;base64,'.base64_encode(\QrCode::format('png')->encoding('UTF-8')->size($size)->margin(0)->errorCorrection('H')->generate($url));
  161. }
  162. //获取浏览器信息
  163. public static function getBrowser()
  164. {
  165. $browser = array('name'=>'unknown', 'version'=>'unknown');
  166. if(empty($_SERVER['HTTP_USER_AGENT'])) return $browser;
  167. $agent = $_SERVER["HTTP_USER_AGENT"];
  168. // Chrome should checked before safari
  169. if(strpos($agent, 'Firefox') !== false) $browser['name'] = "firefox";
  170. if(strpos($agent, 'Opera') !== false) $browser['name'] = 'opera';
  171. if(strpos($agent, 'Safari') !== false) $browser['name'] = 'safari';
  172. if(strpos($agent, 'Chrome') !== false) $browser['name'] = "chrome";
  173. // Check the name of browser
  174. if(strpos($agent, 'MSIE') !== false || strpos($agent, 'rv:11.0')) $browser['name'] = 'ie';
  175. if(strpos($agent, 'Edge') !== false) $browser['name'] = 'edge';
  176. // Check the version of browser
  177. if(preg_match('/MSIE\s(\d+)\..*/i', $agent, $regs)) $browser['version'] = $regs[1];
  178. if(preg_match('/FireFox\/(\d+)\..*/i', $agent, $regs)) $browser['version'] = $regs[1];
  179. if(preg_match('/Opera[\s|\/](\d+)\..*/i', $agent, $regs)) $browser['version'] = $regs[1];
  180. if(preg_match('/Chrome\/(\d+)\..*/i', $agent, $regs)) $browser['version'] = $regs[1];
  181. if((strpos($agent, 'Chrome') == false) && preg_match('/Safari\/(\d+)\..*$/i', $agent, $regs)) $browser['version'] = $regs[1];
  182. if(preg_match('/rv:(\d+)\..*/i', $agent, $regs)) $browser['version'] = $regs[1];
  183. if(preg_match('/Edge\/(\d+)\..*/i', $agent, $regs)) $browser['version'] = $regs[1];
  184. return $browser;
  185. }
  186. /**
  187. * 检查是否是AJAX请求。
  188. * Check is ajax request.
  189. *
  190. * @static
  191. * @access public
  192. * @return bool
  193. */
  194. public static function isAjaxRequest()
  195. {
  196. if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') return true;
  197. if(isset($_GET['HTTP_X_REQUESTED_WITH']) && $_GET['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') return true;
  198. return false;
  199. }
  200. /**
  201. * 检查是否是POST请求
  202. */
  203. public static function isPostRequest()
  204. {
  205. if($_SERVER['REQUEST_METHOD'] == 'POST') return true;
  206. if($_POST) return true;
  207. return false;
  208. }
  209. /**
  210. * 是否是GET提交的
  211. */
  212. public static function isGetRequest()
  213. {
  214. return $_SERVER['REQUEST_METHOD'] == 'GET' ? true : false;
  215. }
  216. /**
  217. * 301跳转。
  218. * Header 301 Moved Permanently.
  219. *
  220. * @param string $locate
  221. * @access public
  222. * @return void
  223. */
  224. public static function header301($locate)
  225. {
  226. header('HTTP/1.1 301 Moved Permanently');
  227. die(header('Location:' . $locate));
  228. }
  229. /**
  230. * 获取远程IP。
  231. * Get remote ip.
  232. *
  233. * @access public
  234. * @return string
  235. */
  236. public static function getRemoteIp()
  237. {
  238. $ip = '';
  239. if(!empty($_SERVER["REMOTE_ADDR"])) $ip = $_SERVER["REMOTE_ADDR"];
  240. if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
  241. if(!empty($_SERVER['HTTP_CLIENT_IP'])) $ip = $_SERVER['HTTP_CLIENT_IP'];
  242. return $ip;
  243. }
  244. /**
  245. * 建立文件夹
  246. *
  247. * @param string $aimUrl
  248. * @return viod
  249. */
  250. public static function createDir($aimUrl)
  251. {
  252. $aimUrl = str_replace('', '/', $aimUrl);
  253. $aimDir = '';
  254. $arr = explode('/', $aimUrl);
  255. $result = true;
  256. foreach ($arr as $str)
  257. {
  258. $aimDir .= $str . '/';
  259. if (!file_exists($aimDir))
  260. {
  261. $result = mkdir($aimDir);
  262. }
  263. }
  264. return $result;
  265. }
  266. //判断访问终端是否是微信浏览器
  267. public static function isWechatBrowser()
  268. {
  269. if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false)
  270. {
  271. return true;
  272. }
  273. return false;
  274. }
  275. //判断是不是https
  276. public static function isHttpsRequest()
  277. {
  278. if((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) return true;
  279. if($_SERVER['SERVER_PORT'] == 443) return true;
  280. return false;
  281. }
  282. /**
  283. * @name php获取中文字符拼音首字母
  284. * @param $str
  285. * @return null|string
  286. */
  287. public function getFirstCharter($str)
  288. {
  289. if (empty($str))
  290. {
  291. return '';
  292. }
  293. $fchar = ord($str{0});
  294. if ($fchar >= ord('A') && $fchar <= ord('z')) return strtoupper($str{0});
  295. $s1 = iconv('UTF-8', 'gb2312', $str);
  296. $s2 = iconv('gb2312', 'UTF-8', $s1);
  297. $s = $s2 == $str ? $s1 : $str;
  298. $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
  299. if ($asc >= -20319 && $asc <= -20284) return 'A';
  300. if ($asc >= -20283 && $asc <= -19776) return 'B';
  301. if ($asc >= -19775 && $asc <= -19219) return 'C';
  302. if ($asc >= -19218 && $asc <= -18711) return 'D';
  303. if ($asc >= -18710 && $asc <= -18527) return 'E';
  304. if ($asc >= -18526 && $asc <= -18240) return 'F';
  305. if ($asc >= -18239 && $asc <= -17923) return 'G';
  306. if ($asc >= -17922 && $asc <= -17418) return 'H';
  307. if ($asc >= -17417 && $asc <= -16475) return 'J';
  308. if ($asc >= -16474 && $asc <= -16213) return 'K';
  309. if ($asc >= -16212 && $asc <= -15641) return 'L';
  310. if ($asc >= -15640 && $asc <= -15166) return 'M';
  311. if ($asc >= -15165 && $asc <= -14923) return 'N';
  312. if ($asc >= -14922 && $asc <= -14915) return 'O';
  313. if ($asc >= -14914 && $asc <= -14631) return 'P';
  314. if ($asc >= -14630 && $asc <= -14150) return 'Q';
  315. if ($asc >= -14149 && $asc <= -14091) return 'R';
  316. if ($asc >= -14090 && $asc <= -13319) return 'S';
  317. if ($asc >= -13318 && $asc <= -12839) return 'T';
  318. if ($asc >= -12838 && $asc <= -12557) return 'W';
  319. if ($asc >= -12556 && $asc <= -11848) return 'X';
  320. if ($asc >= -11847 && $asc <= -11056) return 'Y';
  321. if ($asc >= -11055 && $asc <= -10247) return 'Z';
  322. return '';
  323. }
  324. /**
  325. * 图片转base64
  326. * @param image_file String 图片路径
  327. * @return 转为base64的图片
  328. */
  329. public static function Base64EncodeImage($image_file)
  330. {
  331. if(file_exists($image_file) || is_file($image_file))
  332. {
  333. $base64_image = '';
  334. $image_info = getimagesize($image_file);
  335. $image_data = fread(fopen($image_file, 'r'), filesize($image_file));
  336. $base64_image = 'data:' . $image_info['mime'] . ';base64,' . chunk_split(base64_encode($image_data));
  337. return $base64_image;
  338. }
  339. return false;
  340. }
  341. //提取数字
  342. public static function findNum($str='')
  343. {
  344. $str=trim($str);
  345. if(empty($str)){return '';}
  346. $reg='/(\d{3}(\.\d+)?)/is';//匹配数字的正则表达式
  347. preg_match_all($reg,$str,$result);
  348. if(is_array($result)&&!empty($result)&&!empty($result[1])&&!empty($result[1][0])){
  349. return $result[1][0];
  350. }
  351. return '';
  352. }
  353. /**
  354. * 过滤emoji
  355. */
  356. public static function filterEmoji($str)
  357. {
  358. // preg_replace_callback执行一个正则表达式搜索并且使用一个回调进行替换
  359. $str = preg_replace_callback('/./u', function (array $match) {
  360. return strlen($match[0]) >= 4 ? '' : $match[0];
  361. }, $str);
  362. return $str;
  363. }
  364. //判断是移动端访问
  365. public static function is_mobile_access()
  366. {
  367. // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
  368. if (isset ($_SERVER['HTTP_X_WAP_PROFILE'])) {
  369. return true;
  370. }
  371. //此条摘自TPM智能切换模板引擎,适合TPM开发
  372. if (isset ($_SERVER['HTTP_CLIENT']) && 'PhoneClient' == $_SERVER['HTTP_CLIENT']) {
  373. return true;
  374. }
  375. //如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
  376. if (isset ($_SERVER['HTTP_VIA'])) {
  377. //找不到为flase,否则为true
  378. return stristr($_SERVER['HTTP_VIA'], 'wap') ? true : false;
  379. }
  380. //判断手机发送的客户端标志,兼容性有待提高
  381. if (isset ($_SERVER['HTTP_USER_AGENT'])) {
  382. $clientkeywords = array(
  383. 'nokia', 'sony', 'ericsson', 'mot', 'samsung', 'htc', 'sgh', 'lg', 'sharp', 'sie-', 'philips', 'panasonic', 'alcatel', 'lenovo', 'iphone', 'ipod', 'blackberry', 'meizu', 'android', 'netfront', 'symbian', 'ucweb', 'windowsce', 'palm', 'operamini', 'operamobi', 'openwave', 'nexusone', 'cldc', 'midp', 'wap', 'mobile'
  384. );
  385. //从HTTP_USER_AGENT中查找手机浏览器的关键字
  386. if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
  387. return true;
  388. }
  389. }
  390. //协议法,因为有可能不准确,放到最后判断
  391. if (isset ($_SERVER['HTTP_ACCEPT'])) {
  392. // 如果只支持wml并且不支持html那一定是移动设备
  393. // 如果支持wml和html但是wml在html之前则是移动设备
  394. if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
  395. return true;
  396. }
  397. }
  398. return false;
  399. }
  400. }