PHP URL Shortening Algorithm

后端 未结 7 895
说谎
说谎 2020-12-28 09:22

Could anyone recommend a preferred algorithm to use for URL shortening? I\'m coding using PHP. Initially I thought about writing something that would start at a character su

7条回答
  •  情歌与酒
    2020-12-28 09:36

    Most shortening services just use a counter that is incremented with every entry and convert the base from 10 to 64.

    An implementation in PHP could look like this:

    function encode($number) {
        return strtr(rtrim(base64_encode(pack('i', $number)), '='), '+/', '-_');
    }
    function decode($base64) {
        $number = unpack('i', base64_decode(str_pad(strtr($base64, '-_', '+/'), strlen($base64) % 4, '=')));
        return $number[1];
    }
    
    $number = mt_rand(0, PHP_INT_MAX);
    var_dump(decode(encode($number)) === $number);
    

    The encode function takes an integer number, converts it into bytes (pack), encodes it with the Base-64 encoding (base64_encode), trims the trailing padding = (rtrim), and replaces the characters + and / by - and _ respectively (strtr). The decode function is the inverse function to encode and does the exact opposite (except adding trailing padding).

    The additional use of strtr is to translate the original Base-64 alphabet to the URL and filename safe alphabet as + and / need to be encoded with the Percentage-encoding.

提交回复
热议问题