美文网首页
php生成UUID唯一序列的代码示例

php生成UUID唯一序列的代码示例

作者: geeooooz | 来源:发表于2018-09-13 10:53 被阅读57次
    /**
      * Generates an UUID
      *
      * @author     Anis uddin Ahmad 
      * @param      string  an optional prefix
      * @return     string  the formatted uuid
      */
      function uuid($prefix = '')
      {
        $chars = md5(uniqid(mt_rand(), true));
        $uuid  = substr($chars,0,8) . '-';
        $uuid .= substr($chars,8,4) . '-';
        $uuid .= substr($chars,12,4) . '-';
        $uuid .= substr($chars,16,4) . '-';
        $uuid .= substr($chars,20,12);
        return $prefix . $uuid;
      }  
     
    //Example of using the function -
    //Using without prefix.
    echo uuid(); //Returns like ‘1225c695-cfb8-4ebb-aaaa-80da344e8352′   
     
    //Using with prefix
    echo uuid(‘urn:uuid:’);//Returns like ‘urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344e8352′
    
    

    相关文章

      网友评论

          本文标题:php生成UUID唯一序列的代码示例

          本文链接:https://www.haomeiwen.com/subject/rafwgftx.html