Php code to turn a number to string value

  • Here is a php code to turn a number to it's string value. The number system can be
    from 2 to 52, however the symbols used consist of letters only (English lowercase and uppercase letters, and no numbers). Here is the code snippet:

    <?php function num2str($num, $dim, $size) { if ($dim > 52) return "Dimension is too high error"; $sNum = strval($num); if (preg_match("/E/", $sNum)) return "Out of range error"; $x1 = $size; $temp = ""; echo "on entrance: " . $sNum . "\n"; while (strlen($sNum) - $x1 > 0) { $subS = substr($sNum, strlen($sNum)-$x1,$size); $temp .= num2str($subS, $dim, $size); if (strlen($sNum) - $x1 <= 0){ $temp .= num2str(substr($sNum, 0, $x1), $dim, $size); break; } $x1 += $size; } $dict = array("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", "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"); $digits = array(); while ($num >= $dim) { array_unshift($digits,$num % $dim); $num = floor($num / $dim); } array_unshift($digits, $num); $out = ""; foreach ($digits as $d) { $out .= $dict[$d]; } return $out; } print("result: " . num2str(1111111147483647,52,7) . "\n"); ?>

    The output is as follows:

    on entrance: 1111111147483647
    on entrance: 7483647
    on entrance: 1111114
    result: uOOhEzBLl