If you have large integers and you want to shrink them down in size for whatever reason, you can use this code. Should be easy enough to extend if you want even higher bases (just add a few more chars and increase the base).
<?php
/**
* Converts a base 10 number to any other base.
*
* @param int $val Decimal number
* @param int $base Base to convert to. If null, will use strlen($chars) as base.
* @param string $chars Characters used in base, arranged lowest to highest. Must be at least $base characters long.
*
* @return string Number converted to specified base
*/
function base_encode($val, $base=62, $chars='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') {
if(!isset($base)) $base = strlen($chars);
$str = '';
do {
$m = bcmod($val, $base);
$str = $chars[$m] . $str;
$val = bcdiv(bcsub($val, $m), $base);
} while(bccomp($val,0)>0);
return $str;
}
/**
* Convert a number from any base to base 10
*
* @param string $str Number
* @param int $base Base of number. If null, will use strlen($chars) as base.
* @param string $chars Characters use in base, arranged lowest to highest. Must be at least $base characters long.
*
* @return int Number converted to base 10
*/
function base_decode($str, $base=62, $chars='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') {
if(!isset($base)) $base = strlen($chars);
$len = strlen($str);
$val = 0;
$arr = array_flip(str_split($chars));
for($i = 0; $i < $len; ++$i) {
$val = bcadd($val, bcmul($arr[$str[$i]], bcpow($base, $len-$i-1)));
}
return $val;
}
/**
* Converts a base 10 number to any other base.
*
* @param int $val Decimal number
* @param int $base Base to convert to. If null, will use strlen($chars) as base.
* @param string $chars Characters used in base, arranged lowest to highest. Must be at least $base characters long.
*
* @return string Number converted to specified base
*/
function base_encode($val, $base=62, $chars='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') {
if(!isset($base)) $base = strlen($chars);
$str = '';
do {
$m = bcmod($val, $base);
$str = $chars[$m] . $str;
$val = bcdiv(bcsub($val, $m), $base);
} while(bccomp($val,0)>0);
return $str;
}
/**
* Convert a number from any base to base 10
*
* @param string $str Number
* @param int $base Base of number. If null, will use strlen($chars) as base.
* @param string $chars Characters use in base, arranged lowest to highest. Must be at least $base characters long.
*
* @return int Number converted to base 10
*/
function base_decode($str, $base=62, $chars='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') {
if(!isset($base)) $base = strlen($chars);
$len = strlen($str);
$val = 0;
$arr = array_flip(str_split($chars));
for($i = 0; $i < $len; ++$i) {
$val = bcadd($val, bcmul($arr[$str[$i]], bcpow($base, $len-$i-1)));
}
return $val;
}
(code updated 26-Aug-2011 for arbitrary precision using BC Math)
6 thoughts on “Base62 Encode”
[…] with Base62. I needed classes to handle that in both AS3 and PHP with static access, and based on this snippet I came up with the […]
Base62 encode/decode with AS3 and PHP | BlixtSystems
Hey, thanks for this, it was very helpful! I ported it to javascript. See that version (though a little less flexible) here: https://gist.github.com/1180363
David Crawford
You’re welcome. Glad it was helpful, and nice port!
Mark
[…] http://programanddesign.com/php/base62-encode/ Share this:Like this:LikeBe the first to like this post. This entry was posted in Uncategorized and tagged base62, decode, encode. Bookmark the permalink. ← WCat – Web Capacity Analysis Tool […]
Base62 Encode / Decode | x443
[…] If you want short URLs and predictability is not a concern, you can convert the auto-incrementing ID to a higher base. […]
How do I create unique IDs, like YouTube? - PHP Solutions - Developers Q & A
[…] The following snippet seems like it will do what I need (from http://programanddesign.com/php/base62-encode/) […]
PHP: Short id like Youtube, with salt - PHP Solutions - Developers Q & A