php 插入mongodb uuid类型

dafenqi
2023-08-10 / 0 评论 / 13 阅读 / 正在检测是否收录...

php 插入mongodb uuid类型

  $luuid ='e4eaaaf2-d142-11e1-b3e4-080027620cdd'; //格式一定要正确 » RFC 4122.
  
  $uuid='0123456789abcdef';
  $id = new \MongoDB\BSON\Binary($uuid,\MongoDB\BSON\Binary::TYPE_UUID);

查mongodb的LUUID类型为以下代码:

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

luuid 转为uuid:

  $luuid=二进制字符
  $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);

uuid与luuid互转:

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;
}
0

Deprecated: strtolower(): Passing null to parameter #1 ($string) of type string is deprecated in /www/wwwroot/testblog.58heshihu.com/var/Widget/Archive.php on line 1032

评论 (0)

取消