common.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. <?php
  2. // 应用公共文件
  3. function is_moblie($mobile){
  4. if (!is_numeric($mobile)) {
  5. return false;
  6. }
  7. return preg_match('/^1[3456789]{1}\d{9}$/', $mobile) ? true : false;
  8. }
  9. /**
  10. * 验证手机号是否正确
  11. * @param number $mobile
  12. * @author honfei
  13. */
  14. function isMobile($mobile)
  15. {
  16. if (!is_numeric($mobile)) {
  17. return false;
  18. }
  19. return preg_match('/^1[3456789]{1}\d{9}$/', $mobile) ? true : false;
  20. }
  21. /**
  22. * 中间加密 用正则
  23. */
  24. function encryptTel($tel)
  25. {
  26. $new_tel = preg_replace('/(\d{3})\d{4}(\d{4})/', '$1****$2', $tel);
  27. return $new_tel;
  28. }
  29. /**
  30. * 清除html
  31. * @param $str
  32. * @return mixed|string
  33. */
  34. function clearHtml($str)
  35. {
  36. if (!empty($str)) {
  37. $str = html_entity_decode($str);
  38. $str = strip_tags($str);
  39. $str = str_replace("&nbsp;", "", $str);
  40. }
  41. return $str;
  42. }
  43. /**
  44. * 字符串截取,支持中文和其他编码
  45. * @static
  46. * @access public
  47. * @param string $str 需要转换的字符串
  48. * @param string $start 开始位置
  49. * @param string $length 截取长度
  50. * @param string $charset 编码格式
  51. * @param string $suffix 截断显示字符
  52. * @return string
  53. */
  54. function msubstr($str, $start = 0, $length, $charset = "utf-8", $suffix = true)
  55. {
  56. $_count = hanzi_length($str);
  57. if ($length > $_count && $start == 0) {
  58. $r = $str;
  59. } else {
  60. if (function_exists("mb_substr"))
  61. $slice = mb_substr($str, $start, $length, $charset);
  62. elseif (function_exists('iconv_substr')) {
  63. $slice = iconv_substr($str, $start, $length, $charset);
  64. } else {
  65. $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
  66. $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
  67. $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
  68. $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
  69. preg_match_all($re[$charset], $str, $match);
  70. $slice = join("", array_slice($match[0], $start, $length));
  71. }
  72. $r = $suffix ? $slice . '...' : $slice;
  73. }
  74. return $r;
  75. }
  76. /**
  77. * 计算字符串长度
  78. * @param type $str
  79. * @return int
  80. */
  81. function hanzi_length($str)
  82. {
  83. $len = strlen($str);
  84. $i = 0;
  85. $n = 0;
  86. while ($i < $len) {
  87. if (preg_match("/^[" . chr(0xa1) . "-" . chr(0xff) . "]+$/", $str[$i])) {
  88. $i += 4;
  89. } else {
  90. $i += 1;
  91. }
  92. $n += 1;
  93. }
  94. return $n;
  95. }
  96. /**
  97. * 随机码
  98. * @param type $length
  99. * @return string
  100. */
  101. function randString($length, $c = false)
  102. {
  103. if ($c) {
  104. $_codeSet = '123456789';//不要加0
  105. } else {
  106. $_codeSet = '2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY';
  107. }
  108. $code = '';
  109. for ($i = 0; $i < $length; $i++) {
  110. $code .= $_codeSet[mt_rand(0, strlen($_codeSet) - 1)];
  111. }
  112. return $code;
  113. }
  114. /**
  115. * 验证用户信息返回用户信息
  116. *
  117. */
  118. function is_mobile($user)
  119. {
  120. $r = false;
  121. //验证是否手机号码
  122. $is_mobel = '/^0?(13[0-9]|14[0-9]|15[0-9]|16[0-9]|17[0-9]|18[0-9]|19[[0-9])[0-9]{8}/is';
  123. if (preg_match($is_mobel, $user)) {
  124. $r = true;
  125. }
  126. return $r;
  127. }
  128. function encodePassword($password)
  129. {
  130. return empty($password) ? '' : md5('$^95@Kdf' . md5('Pincll@yiyun' . $password));
  131. }
  132. /**
  133. * 生成用户唯一ip
  134. * @param type $uid
  135. */
  136. function makeUuip($uid){
  137. $codeSert = '123456789ABCDEFGHJKLMNPQRTUVWXY';
  138. $code = '';
  139. $length = 10-strlen($uid)-2;
  140. for ($i = 0; $i < $length; $i++) {
  141. $code .= $codeSert[mt_rand(0, strlen($codeSert) - 1)];
  142. }
  143. return $code."IP".$uid;
  144. }
  145. function makeOrderId($uid,$per="H"){
  146. $codeSert = '123456789ABCDEFGHJKLMNPQRTUVWXY';
  147. $code = '';
  148. $length = 4;
  149. for ($i = 0; $i < $length; $i++) {
  150. $code .= $codeSert[mt_rand(0, strlen($codeSert) - 1)];
  151. }
  152. $time = microtime(true)*10000;
  153. return $per . $time . $code . "OU" .$uid;
  154. }
  155. /**
  156. * 获取唯一token
  157. * @param type $lower 小写
  158. * @return string
  159. */
  160. function getPartToken($lower=false,$sep=true){
  161. $key = "";
  162. if (function_exists ( 'com_create_guid' )) {
  163. $key = com_create_guid();
  164. } else {
  165. //mt_srand((double) microtime() * 10000); //optional for php 4.2.0 and up.
  166. $charid = strtoupper(md5(uniqid(rand(),true))); //根据当前时间(微秒计)生成唯一id.
  167. $hyphen = chr(45); // "-"
  168. $uuid = '' . //chr(123)// "{"
  169. substr($charid,0,8 ).$hyphen.substr($charid,8,4).$hyphen.substr($charid,12,4).$hyphen.substr($charid,16,4).$hyphen.substr($charid,20,12);
  170. //.chr(125);// "}"
  171. $key = $uuid;
  172. }
  173. //小写
  174. if($lower){
  175. $key = strtolower($key);
  176. }
  177. //无分隔符
  178. if(!$sep){
  179. $key = str_replace(chr(45), "", $key);
  180. }
  181. return $key;
  182. }
  183. /**
  184. * 生成UUID
  185. * @param $uid
  186. * @return string
  187. */
  188. function Uuid($uid)
  189. {
  190. $uuid = randString(8,true);
  191. $uuid = substr($uuid,0, - (strlen($uid)+1)) ."0". $uid;
  192. return $uuid;
  193. }
  194. /**
  195. * 生成客服UUID
  196. * @param $uid
  197. * @return string
  198. */
  199. function Kuuid($admin_id)
  200. {
  201. $uuid = randString(8,true);
  202. $uuid = "kf".substr($uuid,0, - (strlen($admin_id)+1)) ."0". $admin_id;
  203. return $uuid;
  204. }
  205. /**
  206. * 生成TUUID
  207. * @param $tuid
  208. * @return string
  209. */
  210. function Tuuid($tuid)
  211. {
  212. $tuuid = randString(8,true);
  213. $tuuid = substr($tuuid,0, - (strlen($tuid)+1))."0".$tuid;
  214. $str = "ABCDEFGHIJKLMNOPQRSTUVWSYZ";
  215. $wordStart = substr($str,mt_rand(0,25),1);
  216. return $wordStart.$tuuid;
  217. }
  218. /**
  219. * 验证身份证
  220. * @param $num
  221. * @return bool
  222. */
  223. function IdentityCard($num) {
  224. return \library\utils\IdentityCard::isValid($num);
  225. }
  226. /**
  227. * 获取客户端IP地址
  228. * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
  229. * @return mixed
  230. */
  231. function get_client_ip($type = 0) {
  232. $type = $type ? 1 : 0;
  233. static $ip = NULL;
  234. if ($ip !== NULL) return $ip[$type];
  235. if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  236. $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  237. $pos = array_search('unknown',$arr);
  238. if(false !== $pos) unset($arr[$pos]);
  239. $ip = trim($arr[0]);
  240. }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
  241. $ip = $_SERVER['HTTP_CLIENT_IP'];
  242. }elseif (isset($_SERVER['REMOTE_ADDR'])) {
  243. $ip = $_SERVER['REMOTE_ADDR'];
  244. }
  245. // IP地址合法验证
  246. $long = sprintf("%u",ip2long($ip));
  247. $ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
  248. return $ip[$type];
  249. }
  250. /**
  251. * 获取客户端信息
  252. */
  253. function getClientInfo(){
  254. $agent = $_SERVER['HTTP_USER_AGENT'];
  255. if(empty($agent)){
  256. return false;
  257. }
  258. $platform="";//平台
  259. $browser="";//内置浏览器版本
  260. //ios系统
  261. if(strpos($agent,'iPad') !== false || strpos($agent,'iPhone') !== false){
  262. $platform = "ios";
  263. }
  264. //android系统
  265. if(strpos($agent,'Android') !== false){
  266. $platform = "android";
  267. }
  268. //微信内置浏览器
  269. if (strpos($agent,'MicroMessenger') !== false ) {
  270. $browser = "weixin";
  271. }
  272. //QQ内置浏览器
  273. if (strpos($agent,'QQ') !== false && strpos($agent,'_SQ_') !== false) {
  274. $browser = "qq";
  275. }
  276. return ["platform"=>$platform,"browser"=>$browser];
  277. }
  278. /**
  279. * 获取随机键值
  280. * @param type $count
  281. * @return type
  282. */
  283. function getRandKeyVal(){
  284. $words = "abcdefghijklmnopqrstuvwxyz";
  285. $key = substr($words,mt_rand(0,25),1).substr($words,mt_rand(0,25),1).mt_rand(0,9);
  286. $val = mt_rand(100,999);
  287. return ["key"=>$key,"val"=>$val];
  288. }
  289. /**
  290. * 不四舍五入保留小数
  291. * @param type $num
  292. * @param type $digit
  293. * @return type
  294. */
  295. function num_min_format($num,$digit=2){
  296. $num = strval(floatval($num));
  297. if(strpos($num,'.')===false){
  298. return floatval($num);
  299. }else{
  300. return floatval(explode(".",$num)[0].".".substr(explode(".",$num)[1],0,$digit));
  301. }
  302. }
  303. /*********************************经纬度计算函数*******************************/
  304. /**
  305. * 获取定位信息
  306. * @param type $lats
  307. * @param type $lngs
  308. * @param type $gps
  309. * @param type $google
  310. * @return type
  311. */
  312. function getgps($lats,$lngs, $gps=false, $google=false)
  313. {
  314. $lat=$lats;
  315. $lng=$lngs;
  316. if($gps)
  317. $c=file_get_contents("http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=$lng&y=$lat");
  318. else if($google)
  319. $c=file_get_contents("http://api.map.baidu.com/ag/coord/convert?from=2&to=4&x=$lng&y=$lat");
  320. else
  321. return array($lat,$lng);
  322. $arr=(array)json_decode($c);
  323. if(!$arr['error'])
  324. {
  325. $lat=base64_decode($arr['y']);
  326. $lng=base64_decode($arr['x']);
  327. }
  328. return array($lat,$lng);
  329. }
  330. /**
  331. * @desc 根据两点间的经纬度计算距离
  332. * @param float $lat 纬度值
  333. * @param float $lng 经度值
  334. */
  335. function getDistance($lat1, $lng1, $lat2, $lng2) {
  336. $earthRadius = 6367000; //approximate radius of earth in meters
  337. $lat1 = ($lat1 * pi() ) / 180;
  338. $lng1 = ($lng1 * pi() ) / 180;
  339. $lat2 = ($lat2 * pi() ) / 180;
  340. $lng2 = ($lng2 * pi() ) / 180;
  341. $calcLongitude = $lng2 - $lng1;
  342. $calcLatitude = $lat2 - $lat1;
  343. $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
  344. $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
  345. $calculatedDistance = $earthRadius * $stepTwo;
  346. return round($calculatedDistance);
  347. }
  348. /**
  349. *获取距离
  350. **/
  351. function getDicAres($size){
  352. $v = $size / 1000;
  353. return number_format($v, 2, '.', '');
  354. }
  355. /**
  356. * 百度转化qq
  357. * @param type $lng
  358. * @param type $lat
  359. */
  360. function bMapTransQQMap($lng, $lat){
  361. $x_pi=3.14159265358979324 * 3000.0/180.0;
  362. $x = $lng - 0.0065;
  363. $y = $lat - 0.006;
  364. $z = sqrt($x * $x + $y * $y) - 0.00002 * sin($y * $x_pi);
  365. $theta = atan2($y, $x) - 0.000003 * cos($x * $x_pi);
  366. $lngs = $z * cos($theta);
  367. $lats = $z * sin($theta);
  368. return array($lngs,$lats);
  369. }
  370. /**
  371. * 获取图片信息
  372. * @param type $url
  373. * @return type
  374. */
  375. function getImageUrlInfo($url){
  376. //图片信息
  377. $imgInfo = @getimagesize($url);
  378. if(!$imgInfo){
  379. return false;
  380. }
  381. $urlAr = explode("/",$url);
  382. $fileName = $urlAr[count($urlAr)-1];
  383. $info=[];
  384. $info["size"] = remote_filesize($url);
  385. $info["name"] = explode(".",$fileName)[0];
  386. // $info["url"] = str_replace("/{$fileName}", "", $url);
  387. $info["url"] = $url;
  388. $info["w"] = $imgInfo[0];
  389. $info["h"] = $imgInfo[1];
  390. $info["ext"] = explode("/",$imgInfo["mime"])[1];
  391. $info["md5"] = md5_file($url);
  392. return $info;
  393. }
  394. // 获取远程文件大小函数
  395. function remote_filesize($url, $user = "", $pw = "")
  396. {
  397. ob_start();
  398. $ch = curl_init($url);
  399. curl_setopt($ch, CURLOPT_HEADER, 1);
  400. curl_setopt($ch, CURLOPT_NOBODY, 1);
  401. if(!empty($user) && !empty($pw))
  402. {
  403. $headers = array('Authorization: Basic ' . base64_encode("$user:$pw"));
  404. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  405. }
  406. $ok = curl_exec($ch);
  407. curl_close($ch);
  408. $head = ob_get_contents();
  409. ob_end_clean();
  410. $regex = '/Content-Length:\s([0-9].+?)\s/';
  411. $count = preg_match($regex, $head, $matches);
  412. return isset($matches[1]) ? intval($matches[1]) : 0;
  413. }