common.php 11 KB

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