function uuid()
{
$chars = md5(uniqid(mt_rand(), true));
$uuid = substr ( $chars, 0, 8 ) . '-'
. substr ( $chars, 8, 4 ) . '-'
. substr ( $chars, 12, 4 ) . '-'
. substr ( $chars, 16, 4 ) . '-'
. substr ( $chars, 20, 12 );
return $uuid ;
}
echo uuid(); //標(biāo)準(zhǔn)的UUID格式為:xxxxxxxx-xxxx-xxxx-xxxxxx-xxxxxxxxxx(8-4-4-4-12)
------------
如果嫌棄結(jié)果太長(zhǎng)可以加入大寫字母
$codes ="qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890_-";
$qr_rand = "";
for ($i=0; $i <16 ; $i++) {
$qr_rand .= $codes[rand(0,63)];
}
// 結(jié)果長(zhǎng)這樣的:7m_2lihOK6ez8YdF
這樣生成16位長(zhǎng)度 每位2的6次方 共16位長(zhǎng)度 16*6 = 2的96次方
比32位md5 2的128次方 差一點(diǎn)
一般情況下夠用了,重復(fù)的概率已經(jīng)非常小了
24位字符的大小寫字母的話會(huì)是2的 24*6 = 144次方,比32位md5 128次方 強(qiáng) 同時(shí)不及160次方 的sha1,但是哈希字符長(zhǎng)度是40
看了一下微信的openid的長(zhǎng)度是29位