要做到這一點在JavaScript也,你需要的http://phpjs.org/ :)
幫助,我想我此腳本中使用的所有PHP函數可用在那裏,就像bcomp
你可以調整這個代碼來獲得一個更小的字符串,現在有點忙,如果我有時間,我一定會更新這個答案:)
<?php
/**
* This function will encode a larger number to small string
**/
function encode($int = null) {
$chars = 'kwn7uh2qifbj8te9vp64zxcmayrg50ds31';
$uid = '';
while(bccomp($int, 0, 0) != 0) {
$rem = bcmod($int, 34);
$int = bcdiv(bcsub($int, $rem, 0), 34, 0);
$uid = $chars[$rem].$uid;
}
return $uid;
}
/**
* This function will decode a string encoded with above function to its original state
**/
function decode($uid = null) {
$chars = 'kwn7uh2qifbj8te9vp64zxcmayrg50ds31';
$id = '';
$len = strlen($uid);
for($i = $len - 1; $i >= 0; $i--) {
$value = strpos($chars, $uid[$i]);
$id = bcadd($id, bcmul($value, bcpow(34, ($len - $i - 1))));
}
return $id;
}
/**
* Below function is only for your needs
**/
function int_to_str($str = null, $decode = false) {
//$len = array(); // reserved for further updates :)
$numbers1 = explode("-", $str);
foreach($numbers1 as &$num1) {
$func = ($decode) ? "decode" : "encode";
$num1 = implode(".", array_map($func, explode(".", $num1)));
}
$numbers1 = implode("-", $numbers1);
return $numbers1;
}
// Encode your numbers to short strings
$str = int_to_str("1372137673.276886940002-19690324617-19694854617-18953258947");
// Decode your encoded string to its original state
$int = int_to_str($str, true);
echo $str."<br />";
echo $int;
?>
你可以嘗試用urlencode()和解碼urldecode() – ripa
你有12個符號編碼(10個數字,短劃線和點)和至少36個可用符號(24個字母,10個數字,短劃線和下劃線)。 **您是否嘗試過使用簡單的地圖(如base64)? –
如果你可以用像'd2rdbt.hfinz2tv-8y84nrx-8y93j9s-8fhkua4'這樣的字符串,那麼我認爲可以提供幫助。因爲代碼有點大 – bystwn22