I was using this algorithm to compress my MongoID to 20 characters from here: Compress (shorten) PHP string from 24 characters to 20 . But, i'm having trouble to reverse it and retrieve back my 24 characters.
Can someone give me some guidelines.
Code that i have used:
$padded = str_repeat('0', 20 - strlen($purchase_id)) . $purchase_id;
$leastSignificant = base_convert(substr($padded, 14, 10), 32, 16); // will be 8 chars most
$middleSignificant = base_convert(substr($padded, 4, 10), 32, 16); // will be 8 chars most
$highSignificant = base_convert(substr($padded, 0, 4), 32, 16); // 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;
I'm getting error: Warning: str_repeat(): Second argument has to be greater than or equal to 0
I got the leastSignificant and highSignificant value correct to my original MongoID. But not the middleSignificant. And i get 25 characters instead of 24.
Got it!
// Pad with 0's to make sure you have 20 chars
$padded = str_repeat('0', 20 - strlen($purchase_id)) . $purchase_id;
$leastSignificant = base_convert(substr($padded, 12, 8), 32, 16); // will be 10 chars most
var_dumped($leastSignificant,false);
$middleSignificant = base_convert(substr($padded, 4, 8), 32, 16); // will be 10 chars most
var_dumped($middleSignificant,false);
$highSignificant = base_convert(substr($padded, 0, 4), 32, 16); // will be 4 chars most
var_dumped($highSignificant,false);
// Concatenate, and make sure everything is correctly padded
$result = str_repeat('0', 4 - strlen($highSignificant)) . $highSignificant .
str_repeat('0', 10 - strlen($middleSignificant )) . $middleSignificant .
str_repeat('0', 10 - strlen($leastSignificant )) . $leastSignificant;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.