<?php
|
|
/**
|
*文件名:UserInfo.php
|
*编写人:杨林康
|
*时间 :2014-04-23
|
*功能 :生成Guid
|
*备注 :使用时GUID::create()生成guid
|
*/
|
namespace App\Utils;
|
|
class guid
|
{
|
public static function create()
|
{
|
$microTime = microtime();
|
list($a_dec, $a_sec) = explode(" ", $microTime);
|
$dec_hex = dechex($a_dec* 1000000);
|
$sec_hex = dechex($a_sec);
|
guid::ensureLength($dec_hex, 5);
|
guid::ensureLength($sec_hex, 6);
|
$guid = "";
|
$guid .= $dec_hex;
|
$guid .= guid::createGuidSection(3);
|
$guid .= '-';
|
$guid .= guid::createGuidSection(4);
|
$guid .= '-';
|
$guid .= guid::createGuidSection(4);
|
$guid .= '-';
|
$guid .= guid::createGuidSection(4);
|
$guid .= '-';
|
$guid .= $sec_hex;
|
$guid .= guid::createGuidSection(6);
|
$ret = str_replace("-","",$guid);
|
return $ret;
|
}
|
|
public static function getGuid(){
|
$microTime = microtime();
|
list($a_dec, $a_sec) = explode(" ", $microTime);
|
$dec_hex = dechex($a_dec* 1000000);
|
$sec_hex = dechex($a_sec);
|
guid::ensureLength($dec_hex, 5);
|
guid::ensureLength($sec_hex, 6);
|
$guid = "";
|
$guid .= $dec_hex;
|
$guid .= guid::createGuidSection(3);
|
$guid .= guid::createGuidSection(4);
|
$guid .= guid::createGuidSection(4);
|
$guid .= guid::createGuidSection(4);
|
$guid .= $sec_hex;
|
$guid .= guid::createGuidSection(6);
|
return $guid;
|
}
|
|
public static function ensureLength(&$string, $length)
|
{
|
$strlen = strlen($string);
|
if($strlen < $length)
|
{
|
$string = str_pad($string,$length,"0");
|
}
|
else if($strlen > $length)
|
{
|
$string = substr($string, 0, $length);
|
}
|
}
|
|
public static function createGuidSection($characters)
|
{
|
$return = "";
|
for($i=0; $i<$characters; $i++)
|
{
|
$return .= dechex(mt_rand(0,15));
|
}
|
return $return;
|
}
|
}
|