How to generate a new GUID?

前端 未结 6 1351
不知归路
不知归路 2020-11-29 02:15

I\'m working on a web service which requires a new GUID() passed as a reference to a method within the service.

I am not familiar with C#

6条回答
  •  臣服心动
    2020-11-29 03:08

    For googlers such as my self, I found this snipet more accurate:

    function getGUID(){
        if (function_exists('com_create_guid')){
            return com_create_guid();
        }else{
            mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
            $charid = strtoupper(md5(uniqid(rand(), true)));
            $hyphen = chr(45);// "-"
            $uuid = chr(123)// "{"
                .substr($charid, 0, 8).$hyphen
                .substr($charid, 8, 4).$hyphen
                .substr($charid,12, 4).$hyphen
                .substr($charid,16, 4).$hyphen
                .substr($charid,20,12)
                .chr(125);// "}"
            return $uuid;
        }
    }
    

    source http://guid.us/GUID/PHP

提交回复
热议问题