php 生成uuid方法

1、自己写函数

/*生成唯一标志
*标准的UUID格式为:xxxxxxxx-xxxx-xxxx-xxxxxx-xxxxxxxxxx(8-4-4-4-12)
*/

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 ;
}

$c=uuid(); //Returns like 'dba5ce3e-430f-cf1f-8443-9b337cb5f7db'
$d=uuid();
echo "$c\n";
echo "$d\n";

2、调用linux命令行

$newId = exec('uuidgen -r');

 

posted @ 2022-08-25 11:44  秦0710  阅读(665)  评论(0编辑  收藏  举报