/ Published in: PHP
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).
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function encode($val, $base=62, $chars='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') { // can't handle numbers larger than 2^31-1 = 2147483647 $str = ''; do { $i = $val % $base; $str = $chars[$i] . $str; $val = ($val - $i) / $base; } while($val > 0); return $str; } function decode($str, $base=62, $chars='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') { $val = 0; for($i = 0; $i < $len; ++$i) { } return $val; } echo encode(2147483647); // outputs 2lkCB1
URL: http://programanddesign.com/php/base62-encode/