48 lines
1.2 KiB
PHP
Executable File
48 lines
1.2 KiB
PHP
Executable File
<?php
|
|
namespace Helpers;
|
|
// 三段
|
|
// 一段是微秒 一段是地址 一段是随机数
|
|
class Uuid
|
|
{
|
|
private $valueBeforeMD5;
|
|
private $valueAfterMD5;
|
|
|
|
function __construct()
|
|
{
|
|
$address = $this->getLocalHost();
|
|
$this->valueBeforeMD5 = $address.':'.$this->currentTimeMillis().':'.$this->nextLong();
|
|
$this->valueAfterMD5 = md5($this->valueBeforeMD5);
|
|
}
|
|
function toString()
|
|
{
|
|
$raw = strtoupper($this->valueAfterMD5);
|
|
return substr($raw,0,8).'-'.substr($raw,8,4).'-'.substr($raw,12,4).'-'.substr($raw,16,4).'-'.substr($raw,20);
|
|
}
|
|
|
|
private function nextLong()
|
|
{
|
|
$tmp = rand(0,1)?'-':'';
|
|
return $tmp.rand(1000, 9999).rand(1000, 9999).rand(1000, 9999).rand(100, 999).rand(100, 999);
|
|
}
|
|
private function currentTimeMillis()
|
|
{
|
|
list($usec, $sec) = explode(" ",microtime());
|
|
return $sec.substr($usec, 2, 3);
|
|
}
|
|
private function getLocalHost()
|
|
{
|
|
$address = isset($_ENV["COMPUTERNAME"]) ? $_ENV["COMPUTERNAME"]."/":"".'/';
|
|
$address.= $_SERVER["SERVER_ADDR"];
|
|
return strtolower($address);
|
|
}
|
|
|
|
static function test($uuid)
|
|
{
|
|
if(!preg_match("/^[0-9A-Za-z]{8}-[0-9A-Za-z]{4}-[0-9A-Za-z]{4}-[0-9A-Za-z]{4}-[0-9A-Za-z]{12}$/",$uuid))
|
|
{
|
|
return false;
|
|
}else{
|
|
return true;
|
|
}
|
|
}
|
|
} |