Skip to content

Commit ca35cb7

Browse files
committed
新增进制转换类
1 parent 722b286 commit ca35cb7

File tree

1 file changed

+221
-13
lines changed

1 file changed

+221
-13
lines changed

common_helper.php

+221-13
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,42 @@ function get_date_str($time, $time_str)
307307
return date("Y-m-d", strtotime('-' . $time . ' days', strtotime($time_str)));
308308
}
309309
}
310+
if (!function_exists('get_distance')) {
311+
/**
312+
* 计算两点地理坐标之间的距离
313+
*
314+
* @param float $longitude1 起点经度
315+
* @param float $latitude1 起点纬度
316+
* @param float $longitude2 终点经度
317+
* @param float $latitude2 终点纬度
318+
* @param Int $unit 单位 1:米 2:公里
319+
* @param Int $decimal 精度 保留小数位数
320+
* @return float
321+
*/
322+
function get_distance($longitude1, $latitude1, $longitude2, $latitude2, $unit = 2, $decimal = 10)
323+
{
324+
325+
$EARTH_RADIUS = 6370.996; // 地球半径系数
326+
327+
$radLat1 = $latitude1 * M_PI / 180.0;
328+
$radLat2 = $latitude2 * M_PI / 180.0;
329+
330+
$radLng1 = $longitude1 * M_PI / 180.0;
331+
$radLng2 = $longitude2 * M_PI / 180.0;
332+
333+
$a = $radLat1 - $radLat2;
334+
$b = $radLng1 - $radLng2;
335+
336+
$distance = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
337+
$distance = $distance * $EARTH_RADIUS;
310338

339+
if ($unit == 1) {
340+
$distance = $distance * 1000;
341+
}
342+
343+
return round($distance, $decimal);
344+
}
345+
}
311346
if (!function_exists('is_date')) {
312347
/**
313348
* 判断是否为日期格式
@@ -321,19 +356,6 @@ function is_date($time)
321356
}
322357
}
323358

324-
if (!function_exists('get_uuid')) {
325-
/**
326-
* 获取唯一id
327-
*
328-
* @param string $prefix
329-
* @return string
330-
*/
331-
function get_uuid($prefix='')
332-
{
333-
return uniqid($prefix, true);
334-
}
335-
}
336-
337359
if (!function_exists('getMillisecond')) {
338360
/**
339361
* 获取毫秒级别的时间戳
@@ -524,6 +546,99 @@ function sign($params, $secret_key)
524546
return md5(http_build_query($params));
525547
}
526548
}
549+
if (!function_exists('desensitize')) {
550+
/**
551+
* 对字符串脱敏
552+
* @param $string
553+
* @param int $start
554+
* @param int $length
555+
* @param string $re
556+
* @return string
557+
*/
558+
function desensitize($string, $start = 0, $length = 0, $re = '*')
559+
{
560+
if (empty($string) || empty($length) || empty($re)) return $string;
561+
$end = $start + $length;
562+
$strlen = mb_strlen($string);
563+
$str_arr = array();
564+
for ($i = 0; $i < $strlen; $i++) {
565+
if ($i >= $start && $i < $end)
566+
$str_arr[] = $re;
567+
else
568+
$str_arr[] = mb_substr($string, $i, 1);
569+
}
570+
return implode('', $str_arr);
571+
}
572+
}
573+
if (!function_exists('is_serialized')) {
574+
/**
575+
* 判断是否虚拟化数据
576+
* @param $data
577+
* @return bool
578+
*/
579+
function is_serialized($data)
580+
{
581+
$data = trim($data);
582+
if ('N;' == $data) {
583+
return true;
584+
}
585+
if (!preg_match('/^([adObis]):/', $data, $badions)) {
586+
return false;
587+
}
588+
switch ($badions[1]) {
589+
case 'a' :
590+
case 'O' :
591+
case 's' :
592+
if (preg_match("/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data)) {
593+
return true;
594+
}
595+
break;
596+
case 'b' :
597+
case 'i' :
598+
case 'd' :
599+
if (preg_match("/^{$badions[1]}:[0-9.E-]+;\$/", $data)) {
600+
return true;
601+
}
602+
break;
603+
}
604+
605+
return false;
606+
}
607+
}
608+
if (!function_exists('get_uuid')) {
609+
/**
610+
* 获取唯一id
611+
*
612+
* @param string $prefix
613+
* @return string
614+
*/
615+
function get_uuid($prefix='')
616+
{
617+
return uniqid($prefix, true);
618+
}
619+
}
620+
621+
if (!function_exists('guid')) {
622+
/**
623+
* 返回guid
624+
* @return string
625+
*/
626+
function guid()
627+
{
628+
if (function_exists('com_create_guid')) {
629+
return com_create_guid();
630+
} else {
631+
mt_srand((double)microtime() * 10000);//optional for php 4.2.0 and up.
632+
$char_id = strtoupper(md5(uniqid(rand(0,getrandmax()), true)));
633+
$hyphen = chr(45);
634+
return substr($char_id, 0, 8) . $hyphen
635+
. substr($char_id, 8, 4) . $hyphen
636+
. substr($char_id, 12, 4) . $hyphen
637+
. substr($char_id, 16, 4) . $hyphen
638+
. substr($char_id, 20, 12);
639+
}
640+
}
641+
}
527642

528643
if (!function_exists('transformTime')) {
529644
/**
@@ -1080,3 +1195,96 @@ function add_tree(&$items, $pid, $name)
10801195
];
10811196
}
10821197
}
1198+
1199+
1200+
if (!function_exists('is_cli')) {
1201+
1202+
function is_cli()
1203+
{
1204+
return PHP_SAPI === 'cli';
1205+
}
1206+
1207+
}
1208+
1209+
if (!function_exists('validation_filter_id_card')) {
1210+
/**
1211+
* 检测身份证是否合法
1212+
* @param $id_card
1213+
* @return bool
1214+
*/
1215+
function validation_filter_id_card($id_card)
1216+
{
1217+
if (strlen($id_card) == 18) {
1218+
return id_card_checksum18($id_card);
1219+
} elseif ((strlen($id_card) == 15)) {
1220+
return id_card_checksum18(id_card_15to18($id_card));
1221+
} else {
1222+
return false;
1223+
}
1224+
}
1225+
}
1226+
if (!function_exists('id_card_verify_number')) {
1227+
/**
1228+
* 计算身份证校验码,根据国家标准GB 11643-1999
1229+
* @param $id_card_base
1230+
* @return false|string
1231+
*/
1232+
function id_card_verify_number($id_card_base)
1233+
{
1234+
if (strlen($id_card_base) != 17) {
1235+
return false;
1236+
}
1237+
//加权因子
1238+
$factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
1239+
//校验码对应值
1240+
$verify_number_list = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
1241+
$checksum = 0;
1242+
for ($i = 0; $i < strlen($id_card_base); $i++) {
1243+
$checksum += substr($id_card_base, $i, 1) * $factor[$i];
1244+
}
1245+
$mod = $checksum % 11;
1246+
$verify_number = $verify_number_list[$mod];
1247+
return $verify_number;
1248+
}
1249+
}
1250+
if (!function_exists('id_card_15to18')) {
1251+
/**
1252+
* 将15位身份证升级到18位
1253+
* @param $id_card
1254+
* @return false|string
1255+
*/
1256+
function id_card_15to18($id_card)
1257+
{
1258+
if (strlen($id_card) != 15) {
1259+
return false;
1260+
} else {
1261+
// 如果身份证顺序码是996 997 998 999,这些是为百岁以上老人的特殊编码
1262+
if (array_search(substr($id_card, 12, 3), array('996', '997', '998', '999')) !== false) {
1263+
$id_card = substr($id_card, 0, 6) . '18' . substr($id_card, 6, 9);
1264+
} else {
1265+
$id_card = substr($id_card, 0, 6) . '19' . substr($id_card, 6, 9);
1266+
}
1267+
}
1268+
$id_card = $id_card . id_card_verify_number($id_card);
1269+
return $id_card;
1270+
}
1271+
}
1272+
if (!function_exists('id_card_checksum18')) {
1273+
/**
1274+
* 18位身份证校验码有效性检查
1275+
* @param $id_card
1276+
* @return bool
1277+
*/
1278+
function id_card_checksum18($id_card)
1279+
{
1280+
if (strlen($id_card) != 18) {
1281+
return false;
1282+
}
1283+
$idcard_base = substr($id_card, 0, 17);
1284+
if (id_card_verify_number($idcard_base) != strtoupper(substr($id_card, 17, 1))) {
1285+
return false;
1286+
} else {
1287+
return true;
1288+
}
1289+
}
1290+
}

0 commit comments

Comments
 (0)