{"id":632,"date":"2015-10-05T16:28:31","date_gmt":"2015-10-05T08:28:31","guid":{"rendered":"http:\/\/www.shuran.cn\/?p=632"},"modified":"2015-10-05T16:28:31","modified_gmt":"2015-10-05T08:28:31","slug":"php%e7%9a%84-uuid%e7%94%9f%e6%88%90%ef%bc%88ci%e7%89%88%e6%9c%ac%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.shuran.cn\/?p=632","title":{"rendered":"PHP\u7684 uuid\u751f\u6210\uff08CI\u7248\u672c\uff09"},"content":{"rendered":"<pre class=\"prettyprint lang-js\">&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');\r\n\/**\r\n * UUID Class\r\n *\r\n * This implements the abilities to create UUID's for CodeIgniter.\r\n * Code has been borrowed from the followinf comments on php.net\r\n * and has been optimized for CodeIgniter use.\r\n * http:\/\/www.php.net\/manual\/en\/function.uniqid.php#94959\r\n *\r\n * @category Libraries\r\n * @author Dan Storm\r\n * @link http:\/\/catalystcode.net\/\r\n * @license GNU LPGL\r\n * @version 2.1 \r\n *\/\r\nclass Uuid \r\n{\r\n    \r\n\tpublic function v3($name, $namespace = null) \r\n\t{    \t\r\n\t\tif(is_null($namespace))\r\n\t\t\t$namespace = $this-&gt;v4();\r\n    \t\r\n\t\tif(empty($name))\r\n\t\t\treturn FALSE;\r\n    \t\r\n\t\tif( ! $this-&gt;is_valid($namespace)) \r\n\t\t\treturn FALSE;\r\n\t\t\/\/ Get hexadecimal components of namespace\r\n\t\t$nhex = str_replace(array('-','{','}'), '', $namespace);\r\n\t\t\/\/ Binary Value\r\n\t\t$nstr = '';\r\n\t\t\/\/ Convert Namespace UUID to bits\r\n\t\tfor($i = 0; $i &lt; strlen($nhex); $i+=2) \r\n\t\t{\r\n\t\t\t$nstr .= chr(hexdec($nhex[$i].$nhex[$i+1]));\r\n\t\t}\r\n\t\t\/\/ Calculate hash value\r\n\t\t$hash = md5($nstr . $name);\r\n\t\treturn sprintf('%08s-%04s-%04x-%04x-%12s',\r\n\t\t\t\/\/ 32 bits for \"time_low\"\r\n\t\t\tsubstr($hash, 0, 8),\r\n\t\r\n\t\t\t\/\/ 16 bits for \"time_mid\"\r\n\t\t\tsubstr($hash, 8, 4),\r\n\t\r\n\t\t\t\/\/ 16 bits for \"time_hi_and_version\",\r\n\t\t\t\/\/ four most significant bits holds version number 3\r\n\t\t\t(hexdec(substr($hash, 12, 4)) &amp; 0x0fff) | 0x3000,\r\n\t\r\n\t\t\t\/\/ 16 bits, 8 bits for \"clk_seq_hi_res\",\r\n\t\t\t\/\/ 8 bits for \"clk_seq_low\",\r\n\t\t\t\/\/ two most significant bits holds zero and one for variant DCE1.1\r\n\t\t\t(hexdec(substr($hash, 16, 4)) &amp; 0x3fff) | 0x8000,\r\n\t\r\n\t\t\t\/\/ 48 bits for \"node\"\r\n\t\t\tsubstr($hash, 20, 12)\r\n\t\t);\r\n  }\r\n\tpublic function v4($trim = false) \r\n\t{\r\n\t\t\r\n\t\t$format = ($trim == false) ? '%04x%04x-%04x-%04x-%04x-%04x%04x%04x' : '%04x%04x%04x%04x%04x%04x%04x%04x';\r\n    \t\r\n\t\treturn sprintf($format,\r\n\t\t\t\/\/ 32 bits for \"time_low\"\r\n\t\t\tmt_rand(0, 0xffff), mt_rand(0, 0xffff),\r\n\t\t\t\/\/ 16 bits for \"time_mid\"\r\n\t\t\tmt_rand(0, 0xffff),\r\n\t\t\t\/\/ 16 bits for \"time_hi_and_version\",\r\n\t\t\t\/\/ four most significant bits holds version number 4\r\n\t\t\tmt_rand(0, 0x0fff) | 0x4000,\r\n\t\t\t\/\/ 16 bits, 8 bits for \"clk_seq_hi_res\",\r\n\t\t\t\/\/ 8 bits for \"clk_seq_low\",\r\n\t\t\t\/\/ two most significant bits holds zero and one for variant DCE1.1\r\n\t\t\tmt_rand(0, 0x3fff) | 0x8000,\r\n\t\t\t\/\/ 48 bits for \"node\"\r\n\t\t\tmt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)\r\n\t\t);\r\n\t}\r\n\tpublic function v5($name, $namespace = null) \r\n\t{\r\n\t\tif(is_null($namespace))\r\n\t\t\t$namespace = $this-&gt;v4();\r\n    \t\r\n\t\tif(empty($name))\r\n\t\t\treturn FALSE;\r\n    \t\r\n\t\tif( ! $this-&gt;is_valid($namespace)) \r\n\t\t\treturn FALSE;\r\n\t\t\/\/ Get hexadecimal components of namespace\r\n\t\t$nhex = str_replace(array('-','{','}'), '', $namespace);\r\n\t\t\/\/ Binary Value\r\n\t\t$nstr = '';\r\n\t\t\/\/ Convert Namespace UUID to bits\r\n\t\tfor($i = 0; $i &lt; strlen($nhex); $i+=2) \r\n\t\t{\r\n\t\t\t$nstr .= chr(hexdec($nhex[$i].$nhex[$i+1]));\r\n\t\t}\r\n\t\t\/\/ Calculate hash value\r\n\t\t$hash = sha1($nstr . $name);\r\n\t\treturn sprintf('%08s-%04s-%04x-%04x-%12s',\r\n\t\t\t\/\/ 32 bits for \"time_low\"\r\n\t\t\tsubstr($hash, 0, 8),\r\n\t\t\t\/\/ 16 bits for \"time_mid\"\r\n\t\t\tsubstr($hash, 8, 4),\r\n\t\t\t\/\/ 16 bits for \"time_hi_and_version\",\r\n\t\t\t\/\/ four most significant bits holds version number 5\r\n\t\t\t(hexdec(substr($hash, 12, 4)) &amp; 0x0fff) | 0x5000,\r\n\t\t\t\/\/ 16 bits, 8 bits for \"clk_seq_hi_res\",\r\n\t\t\t\/\/ 8 bits for \"clk_seq_low\",\r\n\t\t\t\/\/ two most significant bits holds zero and one for variant DCE1.1\r\n\t\t\t(hexdec(substr($hash, 16, 4)) &amp; 0x3fff) | 0x8000,\r\n\t\t\t\/\/ 48 bits for \"node\"\r\n\t\t\tsubstr($hash, 20, 12)\r\n\t\t);\r\n\t}\r\n\tpublic function is_valid($uuid) \r\n\t{\r\n    return preg_match('\/^\\{?[0-9a-f]{8}\\-?[0-9a-f]{4}\\-?[0-9a-f]{4}\\-?[0-9a-f]{4}\\-?[0-9a-f]{12}\\}?$\/i', $uuid) === 1;\r\n\t}\r\n<p>\r\n\t}\r\n<\/p>\r\n<\/pre>\n<p><h1 style=\"font-size:2.25em;margin-left:0px;color:#333333;font-family:'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif;\">\n\t\tCodeIgniter-UUID<br \/>\n\t<\/h1>\n<p style=\"color:#333333;font-family:'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif;font-size:16px;\">\n\t\tCodeIgniter-UUID is a CodeIgniter library enables you to create UUID's within CodeIgniter. Supported versions of UUID is v4 (random), v3 (MD5 hash) and v5 (SHA-1 hash).\n\t<\/p>\n<h2 style=\"font-size:1.75em;color:#333333;font-family:'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif;\">\n\t\t<a id=\"user-content-usage\" class=\"anchor\" href=\"https:\/\/github.com\/psugand\/codeigniter-uuid#usage\"><\/a>Usage<br \/>\n\t<\/h2>\n<pre>\/\/Output a v4 UUID \r\necho $this-&gt;uuid-&gt;v4(); \r\n\r\n\/\/Output a v3 UUID from a name and namespace (if a valid UUID namespace is omitted, a v4 generated UUID will be used)\r\necho $this-&gt;uuid-&gt;v3('My Name', '8d3dc6d8-3a0d-4c03-8a04-1155445658f7'); \r\necho $this-&gt;uuid-&gt;v3('My Name'); \r\n\r\n\/\/Output a v5 UUID from a name and namespace (if a valid UUID namespace is omitted, a v4 generated UUID will be used)\r\necho $this-&gt;uuid-&gt;v5('My Name', '8d3dc6d8-3a0d-4c03-8a04-1155445658f7'); \r\necho $this-&gt;uuid-&gt;v5('My Name');<\/pre>\n<p style=\"color:#333333;font-family:'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif;font-size:16px;\">\n\t\tFor information on UUID, take a look at the following URL:<a href=\"http:\/\/en.wikipedia.org\/wiki\/Universally_unique_identifier\">http:\/\/en.wikipedia.org\/wiki\/Universally_unique_identifier<\/a>\n\t<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&lt;?php if ( ! defined('BASEPATH')) exit('No direct sc\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.shuran.cn\/?p=632\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"class_list":["post-632","post","type-post","status-publish","format-standard","hentry","category-ci"],"_links":{"self":[{"href":"https:\/\/www.shuran.cn\/index.php?rest_route=\/wp\/v2\/posts\/632","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shuran.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shuran.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shuran.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shuran.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=632"}],"version-history":[{"count":1,"href":"https:\/\/www.shuran.cn\/index.php?rest_route=\/wp\/v2\/posts\/632\/revisions"}],"predecessor-version":[{"id":633,"href":"https:\/\/www.shuran.cn\/index.php?rest_route=\/wp\/v2\/posts\/632\/revisions\/633"}],"wp:attachment":[{"href":"https:\/\/www.shuran.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=632"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuran.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=632"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuran.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=632"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}