Base62
필요하신 분은 가져다 쓰세요.
class base62 {
const $charset = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
const $base = 62;
public function encode($var) {
$stack = array();
while($var > 0) {
$remainder = $var % self::$base;
$var = (int)($var / self::$base);
array_push($stack, self::$charset[$remainder]);
}
return implode('', array_reverse($stack));
}
public function decode($var) {
$length = strlen($var);
$result = 0;
for($i=0; $i<$length; $i++) {
$result += self::get_value($var[$i]) * pow(self::$base, ($length-($i+1)));
}
return $result;
}
private function get_value($var) {
if(ereg('[0-9]', $var))
return (int)(ord($var) - ord('0'));
else if(ereg('[A-Z]', $var))
return (int)(ord($var) - ord('A') + 10);
else if(ereg('[a-z]', $var))
return (int)(ord($var) - ord('a') + 36);
else
return $var;
}
}
base62::encode(61);
base62::encode(128);
base62::encode(65536);
base62::encode(2147483647);
base62::decode(base62::encode(51));
base62::decode(base62::encode(255));
base62::decode(base62::encode(33232));
base62::decode(base62::encode(2047343643));
z
24
H32
2LKcb1
51
255
33232
2047343643