• PHP 生成 UUID


    php中生成uuid的方法

    Generate name based md5 UUID (version 3)

    /**
     * Generate name based md5 UUID (version 3).
     * @example '7e57d004-2b97-0e7a-b45f-5387367791cd'
     */
    public function uuid()
    {
        // fix for compatibility with 32bit architecture; seed range restricted to 62bit
        $seed = mt_rand(0, 2147483647) . '#' . mt_rand(0, 2147483647);
    
        // Hash the seed and convert to a byte array
        $val = md5($seed, true);
        $byte = array_values(unpack('C16', $val));
    
        // extract fields from byte array
        $tLo = ($byte[0] << 24) | ($byte[1] << 16) | ($byte[2] << 8) | $byte[3];
        $tMi = ($byte[4] << 8) | $byte[5];
        $tHi = ($byte[6] << 8) | $byte[7];
        $csLo = $byte[9];
        $csHi = $byte[8] & 0x3f | (1 << 7);
    
        // correct byte order for big edian architecture
        if (pack('L', 0x6162797A) == pack('N', 0x6162797A)) {
            $tLo = (($tLo & 0x000000ff) << 24) | (($tLo & 0x0000ff00) << 8)
                | (($tLo & 0x00ff0000) >> 8) | (($tLo & 0xff000000) >> 24);
            $tMi = (($tMi & 0x00ff) << 8) | (($tMi & 0xff00) >> 8);
            $tHi = (($tHi & 0x00ff) << 8) | (($tHi & 0xff00) >> 8);
        }
    
        // apply version number
        $tHi &= 0x0fff;
        $tHi |= (3 << 12);
    
        // cast to string
        $uuid = sprintf(
            '%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x',
            $tLo,
            $tMi,
            $tHi,
            $csHi,
            $csLo,
            $byte[10],
            $byte[11],
            $byte[12],
            $byte[13],
            $byte[14],
            $byte[15]
        );
    
        return $uuid;
    }

    函数来源:fzaninotto/faker

    地址:https://github.com/fzaninotto/Faker

  • 相关阅读:
    vmware磁盘空间扩展
    Winrar发现损坏的压缩文件头
    java ASM动态生成类
    使用ffmpeg将任意格式视频转MP4格式
    mongodb导入csv结构化数据
    Vmware黑屏解决方法
    mysql命令行导入结构化数据
    mysql导入慢解决方法
    CategoryPanelGroup动态生成节点
    delphi XE7 判断手机返回键
  • 原文地址:https://www.cnblogs.com/sirdong/p/12031803.html
Copyright © 2020-2023  润新知