试试这个
function str_split_unicode($str, $l = 0) {
if ($l > 0) {
$ret = array();
$len = mb_strlen($str, "UTF-8");
for ($i = 0; $i < $len; $i += $l) {
$ret[] = mb_substr($str, $i, $l, "UTF-8");
}
return $ret;
}
return preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY);
}
function intToAlphaBaseN($n,$baseArray) {
$l=count($baseArray);
$s = '';
for ($i = 1; bccomp($n , 0) > 0 ; $i = bcadd($i,1)) {
$s = $baseArray[bcdiv(bcmod($n , bcpow($l, $i)) , bcpow($l, bcsub($i , 1)))].$s;
$n = bcsub($n,bcmul(bcdiv(bcmod($n , bcpow($l, $i)) , bcpow($l, bcsub($i , 1))),bcpow($l, bcsub($i , 1))));
bcadd($i,1);
}
return $s;
}
function alphaBaseNToInt($alpha, $baseArray) {
$result = 0;
$l = count($baseArray);
$alpha = str_split_unicode($alpha,1);
$alpha = array_reverse($alpha);
for ($i = count($alpha) - 1; $i >= 0; $i--) {
$result = bcadd($result, bcmul(array_search($alpha[$i],$baseArray) , bcpow($l,$i)));
}
return $result;
}
$alpha=array( '0','1','2','3','4','5','6','7','8','9',
'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');
$listmet = intToAlphaBaseN($string,$alpha);
var_dump($listmet);
$listmet = alphaBaseNToInt($listmet,$alpha);
var_dump($listmet);
我使用了 bcmath 库。因为我正在处理非常非常大的数字。并将 str_split_unicode 用于特殊的土耳其字符。