Helpers.php
2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
if (!function_exists('data_diff')) {
    /**
     * Get the active class if the condition is not falsy
     *
     * @param        $condition
     * @param string $activeClass
     * @param string $inactiveClass
     *
     * @return string
     */
    function data_diff($origin, $target) {
        $origin     =   new DateTime($origin);
        $target     =   new DateTime($target);
        $interval   =   $origin->diff($target);
        $y          =   $interval->format("%y");
        $m          =   $interval->format("%m");
        $d          =   $interval->format("%d");
        $limit      =   '';
        if (!empty($y)) {$limit .= "{$y}年";}
        if (!empty($m)) {$limit .= "{$m}个月";}
        if (!empty($d)) {$limit .= "{$d}天";}
        return $limit;
    }
}
if (!function_exists('coverData')) {
    /**
     * 字符串转换为对应值的字符串
     * @param $str
     * @param $data
     * @return string
     */
    function coverData($str, $data): string
    {
        $arr = explode(',', $str);
        $rs = [];
        foreach ($arr as $value) {
            if (isset($data[$value])) {
                $rs[] = $data[$value];
            }
        }
        return join(' ', $rs);
    }
}
if (!function_exists('crc64Table')) {
    /**
     * @return array
     */
    function crc64Table() {
        $crc64tab = array();
        // ECMA polynomial
        $poly64rev = (0xC96C5795 << 32) | 0xD7870F42;
        // ISO polynomial
        // $poly64rev = (0xD8 << 56);
        for ($i = 0; $i < 256; $i++)
        {
            for ($part = $i, $bit = 0; $bit < 8; $bit++) {
                if ($part & 1) {
                    $part = (($part >> 1) & ~(0x8 << 60)) ^ $poly64rev;
                } else {
                    $part = ($part >> 1) & ~(0x8 << 60);
                }
            }
            $crc64tab[$i] = $part;
        }
        return $crc64tab;
    }
}
if (!function_exists('crc64')) {
    /**
     * @param $string
     * @param $format
     * @return string
     */
    function crc64($string, $format = '%x'){
        static $crc64tab;
        if ($crc64tab === null) {
            $crc64tab = crc64Table();
        }
        $crc = 0;
        for ($i = 0; $i < strlen($string); $i++) {
            $crc = ~$crc;
            $crc = $crc64tab[($crc ^ ord($string[$i])) & 0xff] ^ (($crc >> 8) & ~(0xff << 56));
            $crc = ~$crc;
        }
        return sprintf($format, $crc);
    }
}
if (!function_exists('getToken')) {
    /**
     * @param $key
     * @return void
     */
    function getToken($key){
        $key = mt_rand();
        $hash = hash_hmac("sha1", $v . mt_rand() . time(), $key, true);
        $token = str_replace('=', '', strtr(base64_encode($hash), '+/', '-_'));
    }
}