added base
This commit is contained in:
parent
a95726c5d6
commit
ad68ae3b67
48
include/library/Cana/Base36.php
Normal file
48
include/library/Cana/Base36.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* base62 Decimal encoder
|
||||
*
|
||||
* @author Devin Smith <devin@cana.la>
|
||||
* @date 2009.12.09
|
||||
*
|
||||
* Convert a db index to a shorter one
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
class Cana_Base36 {
|
||||
private static $_chars = '0123456789abcdefghijklmnopqrstuvwxyz';
|
||||
private static $_base = '36';
|
||||
|
||||
// encode the string
|
||||
public static function encode($val) {
|
||||
if ($val) {
|
||||
$str = '';
|
||||
do {
|
||||
$i = $val % self::$_base;
|
||||
$str = self::$_chars[$i] . $str;
|
||||
$val = ($val - $i) / self::$_base;
|
||||
} while($val > 0);
|
||||
return strtoupper($str);
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
// decode the string
|
||||
public static function decode($str) {
|
||||
if ($str) {
|
||||
$str = strtolower($str);
|
||||
$len = strlen($str);
|
||||
$val = 0;
|
||||
$arr = array_flip(str_split(self::$_chars));
|
||||
for($i = 0; $i < $len; ++$i) {
|
||||
$val += (isset($arr[$str[$i]]) ? $arr[$str[$i]] : 0) * pow(self::$_base, $len-$i-1);
|
||||
}
|
||||
return $val;
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user