在 PHP 中设置自定义基地

Setting a custom base in PHP

我正在尝试构建一个包含 31 个字符的自定义基础:

$custom_array = array("0","1","2","3","4","5","6","7","8","9",
"b","c","d","f","g","h","j","k","l","m","n"
,"p","q","r","s","t","v","w","x","y","z");

我从字母表中排除了元音并在开头添加了数字。

嗯,基本上我需要一个类似于 base_convert() 的函数,这样我就可以转换为 base10,反之亦然。

感谢您的帮助。

编辑: 我想出了这个功能,但公认的答案也是一个好方法。谢谢

$basearray = array("0","1","2","3","4","5","6","7","8","9","b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z");

function baseXto10($code,$basearray)
{
    $codearr = array_reverse(str_split($code));
    $charnum = count($basearray);
    $sum = 0;
    $i = 0;
    foreach($codearr as $key => $value)
    {
        $charpos = array_search($basearray, $value);
        $sum+= $charpos * pow($charnum,$i);
        $i++;
    }
    return $sum;
}

您可能想使用 base_convert() 在实际碱基之间进行转换。然后,从 base10(或任何与此相关的)映射初始基数和新基数之间的值。

也许是这样的

function my_base_convert($input, $fromBase) 
{ 
   $baseMapping = [
      '0' => 0,
      'a' => 'b',
      //etc
   ];

   $result = base_convert($input, $fromBase, 10);

   return str_replace(array_keys($baseMapping), array_values($baseMapping), $input);
}

然后简单str_replace