PHP: converting a uuid stored as BINARY(16) in mysql to RFC 4122 format

If you are storing uuid’s are BINARY(16) in the database, then the following will turn the stored id to the “original” format.

$uuidReadable = unpack("h*",$uuid);
$uuidReadable = preg_replace("/([0-9a-f]{8})([0-9a-f]{4})([0-9a-f]{4})([0-9a-f]{4})([0-9a-f]{12})/", "$1-$2-$3-$4-$5", $uuidReadable);
$uuidReadable = array_merge($uuidReadable);
echo "uuid is " . $uuidReadable[0];