4

I'd like to store uuids in the database as BINARY(16) but I need to accept them and present them as the XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX format. Before I jump to split the guid in PHP code (what a fun that is!), are there any libraries or functions that do this out-of-the-box?

Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569
  • [this link is nice](http://stackoverflow.com/questions/10950202/how-to-store-uuid-as-number) – nawfal Jun 27 '12 at 23:52

2 Answers2

5

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

Performs the inverse operation of HEX(str). That is, it interprets each pair of hexadecimal digits in the argument as a number and converts it to the character represented by the number.
VolkerK
  • 95,432
  • 20
  • 163
  • 226
  • +1 There may be a library, but this is a short task for just converting them. Unless you're generating them, I'd just spend the few extra lines to code your own function. – Jeff Ferland Aug 27 '09 at 14:36
0

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 it works automatically for CHAR(36) and BINARY(36) from what I understand. Either way you could dig into the source a bit to see if it has what you need.

Edit: Here is a stand alone class that can generate and convert into just about any format you might need: http://www.shapeshifter.se/2008/09/29/uuid-generator-for-php/ I posted this in the below comment but wanted to make sure the link is easily visible for other users.

Kyle Kochis
  • 649
  • 4
  • 16
  • No, I mean BINARY(16). I want to store the underlying value, not the expanded character representation. – Remus Rusanu Aug 27 '09 at 05:03
  • OK I see. The PECL library should do it for you but this might be better: http://www.shapeshifter.se/2008/09/29/uuid-generator-for-php/ It not only generates UUIDs but can also convert to different formats including BINARY(16) – Kyle Kochis Aug 27 '09 at 05:39