note 99783 deleted from function.uniqid by thiago

From: Date: Tue, 07 Sep 2010 04:52:45 +0000
Subject: note 99783 deleted from function.uniqid by thiago
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-172352@lists.php.net to get a copy of this message
Note Submitter: Hans ---- 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); }

« previous php.notes (#172352) next »