/ Published in: PHP
URL: http://programanddesign.com/php/base62-encode/
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
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; }
Comments
Subscribe to comments
You need to login to post a comment.

Found this snippet from a link from StackOverflow.
If you want to encode larger numbers (like I did) you need to replace...
$val % $base...with...fmod($val, $base)