base62_encode · PHP Function to Encode String in Base62

Home
function base62_encode ($data) {
    $outstring = '';
    $len = strlen($data);
    for ($i = 0; $i < $len; $i += 8) {
        $chunk = substr($data, $i, 8);
        $outlen = ceil((strlen($chunk) * 8) / 6);
        $x = bin2hex($chunk);
        $number = ltrim($x, '0');
        if ($number === '') $number = '0';
        $w = gmp_strval(gmp_init($number, 16), 62);
        $pad = str_pad($w, $outlen, '0', STR_PAD_LEFT);
        $outstring .= $pad;
    }
    return $outstring;
}
Last updated 2 years ago.
nichabi.com