PHP inserts the mongodb uuid type

PHP inserts the mongodb uuid type

$luuid ='e4eaaaf2-d142-11e1-b3e4-080027620cdd'; //The format must be correct » RFC 4122.

$uuid='0123456789abcdef';
$id = new \MongoDB\BSON\Binary($uuid,\MongoDB\BSON\Binary::TYPE_UUID);

Query mongodb’s LUUID type as the following code:

$luuid= str_replace("-","",$luuid);
$id= new \MongoDB\BSON\Binary(hex2bin($luuid),\MongoDB\BSON\Binary::TYPE_OLD_UUID);
$id=bin2hex($id);
var_dump($id);

luuid to uuid:

$luuid=Binary characters
$id=bin2hex($luuid);
$a = substr($id,6, 2).substr($id,4, 2).substr($id,2, 2).substr($id,0, 2);
$b = substr($id,10, 2).substr($id,8, 2);
$c = substr($id,14, 2).substr($id,12, 2);
$d = substr($id,16, 16);
$id = $a.$b.$c.$d;
$id = substr($id,0, 8).'-'.substr($id,8, 4).'-'.substr($id,12, 4).'-'.substr($id,16, 4).'-'.substr($id,20, 12);
var_dump($id);

Luuid and uuid Convert to each other:

publicfunctionreplayLuuid($id,$type)
{
$id = str_replace("-","",$id);
$a = substr($id,6, 2).substr($id,4, 2).substr($id,2, 2).substr($id,0, 2);
$b = substr($id,10, 2).substr($id,8, 2);
$c = substr($id,14, 2).substr($id,12, 2);
$d = substr($id,16, 16);
$id = $a.$b.$c.$d;
if ($type =='to') {
$id= new \MongoDB\BSON\Binary(hex2bin($id),\MongoDB\BSON\Binary::TYPE_OLD_UUID);
}else{
$id = substr($id,0, 8).'-'.substr($id,8, 4).'-'.substr($id,12, 4).'-'.substr($id,16, 4).'-'.substr($id,20, 12);
}
return $id;
}

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *