Skip to content

Instantly share code, notes, and snippets.

@imzhi
Created February 15, 2016 02:45
Show Gist options
  • Select an option

  • Save imzhi/5d08010b25a5b9e28d9d to your computer and use it in GitHub Desktop.

Select an option

Save imzhi/5d08010b25a5b9e28d9d to your computer and use it in GitHub Desktop.
生成uuid
<?php
// UUID百度百科<http://baike.baidu.com/view/1052579.htm>
// 8-4-4-4-12
function create_uuid()
{
$str = md5(uniqid('', true));
return preg_replace('/(\w{8})(\w{4})(\w{4})(\w{4})(\w{12})/', '$1-$2-$3-$4-$5', $str);
}
@imzhi
Copy link
Author

imzhi commented Feb 15, 2016

直观方法:

<?php
// UUID百度百科<http://baike.baidu.com/view/1052579.htm>
// 8-4-4-4-12
function create_uuid()
{
    $str = md5(uniqid('', true));
    $sep = '-';
    return substr($str, 0, 8) . $sep . substr($str, 8, 4) . $sep . substr($str, 12, 4) . $sep . substr($str, 16, 4) . $sep . substr($str, 12);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment