sql >> Databáze >  >> NoSQL >> MongoDB

Komprimujte (zkraťte) PHP řetězec z 24 znaků na 20

Z toho, co vidím na vaší odkazované stránce, je 24 znaků hexadecimálních. Pokud může být ID zákazníka alfanumerické, můžete použít base_convert ke zkrácení čísla. Bohužel plný počet je> 32bit, takže jej musíte po částech zakrýt, aby to fungovalo:

// Pad with 0's to make sure you have 24 chars
$padded = str_repeat('0', 24 - strlen($mongoId)) . $mongoId;
$leastSignificant = base_convert(substr($padded, 14, 10), 16, 32); // will be 8 chars most
$middleSignificant = base_convert(substr($padded, 4, 10), 16, 32); // will be 8 chars most
$highSignificant = base_convert(substr($padded, 0, 4), 16, 32); // will be 4 chars most

// Concatenate, and make sure everything is correctly padded
$result = str_repeat('0', 4 - strlen($highSignificant)) . $highSignificant .
          str_repeat('0', 8 - strlen($middleSignificant )) . $middleSignificant .
          str_repeat('0', 8 - strlen($leastSignificant )) . $leastSignificant;
echo strlen($result); // Will echo 20

// Reverse the algoritm to retrieve the mongoId for a given customerId 



  1. jak používat mapreduce v dílčím dokumentu dotazu mongoose/mongodb?

  2. Mongodb upsert vložený dokument

  3. Převeďte datum ISO do formátu rrrr-mm-dd

  4. Smazat dokument z mongoDB