Create unique string from id to build an link shortening system I want to build my own link shortening system. Therefore I need a way to create unique strings from the record ids stored in my database table.
So far I came up with this. I wonder if you now a faster or better idea? Or are there any caveats to my method?
// generate unique alphanumeric string from id with map
function idToString($id) { 
    $map = &quot;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789&quot;; # 62 characters (a-z;A-Z;0-9)
    $string='';
    if ($id&lt;62) return $map[$id]; // return immediately
    while ($id&gt;0) { 
        if ($id%62&gt;=0) {
            $string=$string.$map[$id%62];
            $id=intval($id/62); 
        }
    } 
    $string=strrev($string);     // Reverse string to emulate human reading
    return $string; 
}

// get integer ID back from a String
function stringToID($string) { 
    $map = str_split(&quot;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789&quot;); # same map as above of course
    $string=strrev($string);
    $string_array=str_split($string);
    $id=0;
    foreach ($string_array as $k=&gt;$c) {
        $id=$id+(array_keys($map, $c)[0])*pow(62, $k);
    }
    return $id;
}

echo idToString(247698); // bcBi
echo stringToID('bcBi'); // 247698
I played with your code for a while, declared constants for your functions to use so that the static values didn't need to be regenerated with each call, added spacing around operators, curly braces for condition blocks, simplified expressions, reduced the total number of function calls, and generally made things more direct.
One point of concern is that you are using arithmetic to translate the values -- for this reason you will need to be wary of the upper limit for your system.  It would be a good idea to throw an exception when your calculation breaks the int max limit.  Also, scientific notation can monkey wrench things too.  I used number_format() to prevent scientific notation while I was testing -- I don't know how much extra mileage this will grant you.  This has been a fun rabbit hole to go down.
define('ALNUM_MAP', str_split('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'));
define('DIGIT_MAP', array_flip(ALNUM_MAP));

function idToString($id) {
    if ($id &lt; 62) {
        return ALNUM_MAP[$id];
    }
    $string = '';
    while ($id) {
        $id = number_format($id, 0, '', '');
        $string = ALNUM_MAP[$id % 62] . $string;
        $id = (int)($id / 62); 
    }
    return $string;
}

function stringToID($string) {
    $id = 0;
    for ($i = strlen($string) - 1, $pow = 0; $i &gt;= 0; --$i, ++$pow) {
        $id += DIGIT_MAP[$string[$i]] * (62 ** $pow);
    }
    return number_format($id, 0, '', '');
}

echo idToString(247698); // bcBi
echo &quot;\n---\n&quot;;
echo stringToID('bcBi'); // 247698
echo &quot;\n---\n&quot;;
//echo idToString(32611315596050636800); // M1ckM4ckUS4
//echo &quot;\n---\n&quot;;
//echo stringToID('M1ckM4ckUS4'); // 32611315596050636800
//echo &quot;\n---\n&quot;;
echo idToString(8483692923010050); // M1ckM4ckU
echo &quot;\n---\n&quot;;
echo stringToID('M1ckM4ckU'); // 8483692923010050


That said, since you are starting with fully numeric values and you want to reduce their character count by converting to an alphanumeric string, then I have a far simpler way based on this answer that I post on Stack Overflow a couple years back.  (Be sure that the incoming values are string-type, not integer-type.)
Code: (Demo)
define('ALNUM_MAP', str_split('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'));
define('DIGIT_MAP', array_flip(ALNUM_MAP));

function numToAlnum($num) {
    return strtr($num, ALNUM_MAP);
}

function alnumToNum($alphaNum) {
    return strtr($alphaNum, DIGIT_MAP);
}

$ids = ['4440', '464426', '43304441302845', '262018191701180', '273381111186152545352'];
foreach ($ids as $id) {
    $idLength = strlen($id);
    $alphaNum = numToAlnum($id);
    $alNumLength = strlen($alphaNum);
    $backToId = alnumToNum($alphaNum);
    var_export(['original' =&gt; $id, 'encoded' =&gt; $alphaNum, 'decoded' =&gt; $backToId, 'reduction' =&gt; $idLength - $alNumLength]);
    echo &quot;\n---\n&quot;;
}

As stated in my SO post, using strtr() is a fantastic technique because it is designed to translate the longest substring that it can find as it traverses the input string.  This translation operates on a different algorithm, so it will not give exactly the same length reduction as your technique, but it will not suffer from any numeric/math-based side-effects either.