[英]Reverse algorithm
I was using this algorithm to compress my MongoID to 20 characters from here: Compress (shorten) PHP string from 24 characters to 20 . 我使用此算法从此处将MongoID压缩为20个字符: 将PHP字符串从24个字符压缩(缩短)为20个字符 。 But, i'm having trouble to reverse it and retrieve back my 24 characters.
但是,我在扭转它并找回我的24个字符方面遇到困难。
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
我收到错误消息:
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. 我得到了与原始MongoID正确的MinimumSignificant和highSignificant值。 But not the middleSignificant.
但不是中间重要的。 And i get 25 characters instead of 24.
我得到25个字符,而不是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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.