hy
zhoudw
2021-12-27 46b64572601f9a18143ad63f777181f4b884396e
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
<?php
 
/**
 *文件名:UserInfo.php
 *编写人:杨林康
 *时间    :2014-04-23
 *功能    :生成Guid
 *备注    :使用时GUID::create()生成guid
 */
namespace App\Utils;
 
class guid
{
    public static function create()
    {
        $microTime = microtime();
        list($a_dec, $a_sec) = explode(" ", $microTime);
        $dec_hex = dechex($a_dec* 1000000);
        $sec_hex = dechex($a_sec);
        guid::ensureLength($dec_hex, 5);
        guid::ensureLength($sec_hex, 6);
        $guid = "";
        $guid .= $dec_hex;
        $guid .= guid::createGuidSection(3);
        $guid .= '-';
        $guid .= guid::createGuidSection(4);
        $guid .= '-';
        $guid .= guid::createGuidSection(4);
        $guid .= '-';
        $guid .= guid::createGuidSection(4);
        $guid .= '-';
        $guid .= $sec_hex;
        $guid .= guid::createGuidSection(6);
        $ret = str_replace("-","",$guid);
        return $ret;
    }
 
    public static function getGuid(){
        $microTime = microtime();
        list($a_dec, $a_sec) = explode(" ", $microTime);
        $dec_hex = dechex($a_dec* 1000000);
        $sec_hex = dechex($a_sec);
        guid::ensureLength($dec_hex, 5);
        guid::ensureLength($sec_hex, 6);
        $guid = "";
        $guid .= $dec_hex;
        $guid .= guid::createGuidSection(3);
        $guid .= guid::createGuidSection(4);
        $guid .= guid::createGuidSection(4);
        $guid .= guid::createGuidSection(4);
        $guid .= $sec_hex;
        $guid .= guid::createGuidSection(6);
        return $guid;
    }
    
    public static function ensureLength(&$string, $length)
    {
        $strlen = strlen($string);
        if($strlen < $length)
        {
            $string = str_pad($string,$length,"0");
        }
        else if($strlen > $length)
        {
            $string = substr($string, 0, $length);
        }
    }
    
    public static function createGuidSection($characters)
    {
        $return = "";
        for($i=0; $i<$characters; $i++)
        {
        $return .= dechex(mt_rand(0,15));
        }
        return $return;
    }
}