[英]Reverse algorithm
我使用此算法從此處將MongoID壓縮為20個字符: 將PHP字符串從24個字符壓縮(縮短)為20個字符 。 但是,我在扭轉它並找回我的24個字符方面遇到困難。
有人可以給我一些指導。
我使用的代碼:
$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;
我收到錯誤消息: Warning: str_repeat(): Second argument has to be greater than or equal to 0
我得到了與原始MongoID正確的MinimumSignificant和highSignificant值。 但不是中間重要的。 我得到25個字符,而不是24個字符。
得到它了!
// 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.