sql >> Base de Datos >  >> NoSQL >> MongoDB

Comprimir (acortar) cadena PHP de 24 caracteres a 20

Por lo que veo en su página de referencia, los 24 caracteres son hexadecimales. Si la identificación del cliente puede ser alfanumérica, puede usar base_convert para acortar el número. Desafortunadamente, el número completo es> 32 bits, por lo que debe convertirlo en partes para que funcione:

// 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