sql >> Database teknologi >  >> NoSQL >> MongoDB

Komprimer (forkort) PHP-streng fra 24 tegn til 20

Ud fra hvad jeg kan se på din refererede side, er de 24 tegn hexadecimale. Hvis kunde-id'et kan være alfanumerisk, kan du bruge base_convert for at forkorte antallet. Desværre er det fulde nummer> 32bit, så du skal skjule det i dele for at få det til at fungere:

// 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. Mongodb upsert kaster DuplicateKeyException

  2. Hvordan forhindrer man opdateringsfunktionsindsættelse til MongoDB fra Meteor?

  3. MongoDB:Er masseoperationer skrevet til oploggen som helhed?

  4. Importer CSV-filer fra en mappe for at seed en database på Rails