Guid / Uuid in PHP with MySQL

I would like to save uuids in the database as BINARY (16), but I need to accept them and present them as format XXXXXXXX-XXXX-XXXXXXXX. Before I move on to guid separation in PHP code (which is fun!), Are there any libraries or functions that do this out of the box?

+4
source share
2 answers

You can remove the dash first with Replace () and then use Unhex ()

Performs the inverse of the HEX (str) operation. That is, he interprets each pair of hexadecimal digits in the argument as a number and converts it to the character represented by the number.
+5
source

Don't you mean BINARY (36) instead of BINARY (16)

Try http://pecl.php.net/package/uuid

CakePHP has some neat functionality for uuids and automatically works for CHAR (36) and BINARY (36) from what I understand. In any case, you could delve into the source a bit to find out if it has what you need.

Edit: here is a standalone class that can generate and convert to almost any format you may need: http://www.shapeshifter.se/2008/09/29/uuid-generator-for-php/ I posted this in the following comments, but I wanted to make sure that the link is easily visible to other users.

0
source

All Articles