实际场景中,不想吧ID暴露出去,就这么转换。如有需要可以用md5新增校验位。
public static function int_2_str($i,$min_length = 4){
$str = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T',
'U','V','W','X','Y','Z'];
$l = count($str);
$b_arr = [];
while(True){
$s = intdiv($i, $l); //商
$y= $i % $l; # 余数
$b_arr[] = $y;
if($s == 0){
break;
}
$i=$s;
}
$b_arr = array_reverse($b_arr);
$re_str = "";
foreach($b_arr as $item){
$re_str = $re_str.$str[$item];
}
$re_str = strtoupper($re_str);
if(strlen($re_str) < $min_length){
$need = $min_length - strlen($re_str);
for($ii =0;$ii<$need;$ii++){
$re_str = $re_str.strtolower($str[random_int(0,$l-1)]);
}
}
return $re_str;
}
public static function str_2_int($str = ''){
$arr = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T',
'U','V','W','X','Y','Z'];
$len = count($arr);
$l = strlen($str);
$arr_new = [];
for($ii = 0;$ii<$l;$ii++){
if (in_array($str[$ii],$arr)){
$arr_new[] = $str[$ii];
}
}
$l_new = count($arr_new);
$re_int = 0;
$arr_new = array_reverse($arr_new);
for($i=0;$i<$l_new;$i++){
$index = array_search($arr_new[$i],$arr);
$re_int = $re_int + $index * pow($len,$i);
}
return $re_int;
}