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(); //标准的UUID格式为:xxxxxxxx-xxxx-xxxx-xxxxxx-xxxxxxxxxx(8-4-4-4-12)
------------
如果嫌弃结果太长可以加入大写字母
$codes ="qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890_-";
$qr_rand = "";
for ($i=0; $i <16 ; $i++) {
$qr_rand .= $codes[rand(0,63)];
}
// 结果长这样的:7m_2lihOK6ez8YdF
这样生成16位长度 每位2的6次方 共16位长度 16*6 = 2的96次方
比32位md5 2的128次方 差一点
一般情况下够用了,重复的概率已经非常小了
24位字符的大小写字母的话会是2的 24*6 = 144次方,比32位md5 128次方 强 同时不及160次方 的sha1,但是哈希字符长度是40
看了一下微信的openid的长度是29位