note 99783 added to function.uniqid
| From: | Hans at osu1 dot php dot net | Date: | Mon, 06 Sep 2010 18:08:09 +0000 |
| Subject: | note 99783 added to function.uniqid | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-172341@lists.php.net to get a copy of this message | ||
I just used the MySQL UUID() function as suggested by another commenter. However, dashed hex strings
are kind of silly index columns when you've got binary fields. Here're some concise
utilities for going back and forth. Not sure if it's worth the round trip time (SELECT ->
convert -> INSERT). If anyone knows a way to shorten up the second one, I'd love to know!
/**
* Converts a default textual representation of a UUID to a binary one.
*
* @param string $uuid
* @return string A 16-byte binary string.
*/
function uuid2bin($uuid)
{
return implode('', array_map('chr', sscanf(
str_replace('-', '', $uuid),
str_repeat('%2x', 16)
)));
}
/**
* Converts a binary representation of a UUID to a default textual one.
*
* @param string $bin A 16-byte binary string.
* @return string
*/
function bin2uuid($bin)
{
$uuidWithoutDashes = str_split(bin2hex($bin), 2);
$uuidParts = array();
foreach (array(4, 2, 2, 2, 6) as $byteCount)
$uuidParts[] = implode('', array_splice($uuidWithoutDashes, 0, $byteCount));
return implode('-', $uuidParts);
}
----
Server IP: 64.71.164.2
Probable Submitter: 208.85.207.115
----
Manual Page -- http://www.php.net/manual/en/function.uniqid.php
Edit -- https://master.php.net/note/edit/99783
Del: integrated -- https://master.php.net/note/delete/99783/integrated
Del: useless -- https://master.php.net/note/delete/99783/useless
Del: bad code -- https://master.php.net/note/delete/99783/bad+code
Del: spam -- https://master.php.net/note/delete/99783/spam
Del: non-english -- https://master.php.net/note/delete/99783/non-english
Del: in docs -- https://master.php.net/note/delete/99783/in+docs
Del: other reasons-- https://master.php.net/note/delete/99783
Reject -- https://master.php.net/note/reject/99783
Search -- https://master.php.net/manage/user-notes.php