本文介绍了PHP函数生成v4 UUID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 所以我一直在做一些挖掘工作,我一直在拼凑一个可以在PHP中生成有效v4 UUID的函数。这是我能够最接近的。我在十六进制,十进制,二进制,PHP的按位运算符等方面的知识几乎不存在。该功能可以生成有效的v4 UUID,直到一个区域。 v4 UUID应采用以下形式: 其中 y 是8,9,A或B.是功能失败的地方,因为它不符合这一点。 我希望在这方面有比我更多知识的人可以借我一把,帮助我修复这个函数所以它坚持那个规则。 函数如下: 函数gen_uuid(){ $ uuid = array('time_low'=> 0,'time_mid' = 0,'time_hi'= 0,'clock_seq_hi'= 0,'clock_seq_low'= 0,'节点'=> ; array()); $ uuid ['time_low'] = mt_rand(0,0xffff)+(mt_rand(0,0xffff)<< 16); $ uuid ['time_mid'] = mt_rand(0,0xffff); $ uuid ['time_hi'] =(4 $ uuid ['clock_seq_hi'] =(1 $ uuid ['clock_seq_low'] = mt_rand(0,255); ($ i = 0; $ i $ uuid ['node'] [$ i] = mt_rand(0,255); 。 $ uuid = sprintf('%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x', $ uuid ['time_low'], $ uuid ['time_mid'], $ uuid ['time_hi'], $ uuid ['clock_seq_hi'], $ uuid ['clock_seq_low'], $ uuid ['node'] [0], $ uuid ['node'] [1], $ uuid ['node'] [2 ], $ uuid ['node'] [3], $ uuid ['node'] [4], $ uuid ['node'] [5] ); 返回$ uuid; } ?> 感谢任何能够帮助我的人。解决方案摘自 this 在PHP手册上发表评论,你可以使用这个: $ p $函数gen_uuid(){ return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', // 32位为time_low mt_rand(0,0xffff),mt_rand(0, // 16位为time_mid mt_rand(0,0xffff), // 16位为time_hi_and_version, //四个最高有效位保存版本号4 mt_rand(0,0x0fff)| 0x4000, // 16位,clk_seq_hi_res的8位, // 8位为clk_seq_low, //两个最高有效位为0,变量DCE1.1 mt_rand(0,0x3fff)| 0x8000, // 48比特为节点 mt_rand(0,0xffff),mt_rand(0,0xffff),mt_rand(0,0xffff)); } So I've been doing some digging around and I've been trying to piece together a function that generates a valid v4 UUID in PHP. This is the closest I've been able to come. My knowledge in hex, decimal, binary, PHP's bitwise operators and the like is nearly non existant. This function generates a valid v4 UUID up until one area. A v4 UUID should be in the form of:where y is 8, 9, A, or B. This is where the functions fails as it doesn't adhere to that.I was hoping someone with more knowledge than me in this area could lend me a hand and help me fix this function so it does adhere to that rule.The function is as follows:<?phpfunction gen_uuid() { $uuid = array( 'time_low' => 0, 'time_mid' => 0, 'time_hi' => 0, 'clock_seq_hi' => 0, 'clock_seq_low' => 0, 'node' => array() ); $uuid['time_low'] = mt_rand(0, 0xffff) + (mt_rand(0, 0xffff) << 16); $uuid['time_mid'] = mt_rand(0, 0xffff); $uuid['time_hi'] = (4 << 12) | (mt_rand(0, 0x1000)); $uuid['clock_seq_hi'] = (1 << 7) | (mt_rand(0, 128)); $uuid['clock_seq_low'] = mt_rand(0, 255); for ($i = 0; $i < 6; $i++) { $uuid['node'][$i] = mt_rand(0, 255); } $uuid = sprintf('%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x', $uuid['time_low'], $uuid['time_mid'], $uuid['time_hi'], $uuid['clock_seq_hi'], $uuid['clock_seq_low'], $uuid['node'][0], $uuid['node'][1], $uuid['node'][2], $uuid['node'][3], $uuid['node'][4], $uuid['node'][5] ); return $uuid;}?>Thanks to anyone that can help me out. 解决方案 Taken from this comment on the PHP manual, you could use this:function gen_uuid() { return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', // 32 bits for "time_low" mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), // 16 bits for "time_mid" mt_rand( 0, 0xffff ), // 16 bits for "time_hi_and_version", // four most significant bits holds version number 4 mt_rand( 0, 0x0fff ) | 0x4000, // 16 bits, 8 bits for "clk_seq_hi_res", // 8 bits for "clk_seq_low", // two most significant bits holds zero and one for variant DCE1.1 mt_rand( 0, 0x3fff ) | 0x8000, // 48 bits for "node" mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ) );} 这篇关于PHP函数生成v4 UUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-14 18:32