php生成用户唯一标识,唯一码,唯一标识

php生成唯一码,唯一标识:

开箱即用,上代码:

function uuid($length=10)
{
    // fix for compatibility with 32bit architecture; each mt_rand call is restricted to 32bit
    // two such calls will cause 64bits of randomness regardless of architecture
    $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]
    );

    $str = strtoupper($uuid);
    while (1){
        $uuid = substr(str_shuffle(str_repeat($str, $length)), 0, $length);
        if(!is_numeric($uuid)){
            break;
        }
    }


    return $uuid;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值