PHP function to generate v4 UUID

前端 未结 15 1068
眼角桃花
眼角桃花 2020-11-22 03:01

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

15条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 03:33

    From tom, on http://www.php.net/manual/en/function.uniqid.php

    $r = unpack('v*', fread(fopen('/dev/random', 'r'),16));
    $uuid = sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
        $r[1], $r[2], $r[3], $r[4] & 0x0fff | 0x4000,
        $r[5] & 0x3fff | 0x8000, $r[6], $r[7], $r[8])
    

提交回复
热议问题