-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnyBase.php
32 lines (31 loc) · 1002 Bytes
/
AnyBase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
/**
* AnyBase - create a base numbering system to meet your needs.
* Designed to create short urls based on a decimal id number - the default
* charset contains the unreserved URL characters and is good up to base65.
*
* @author Michael Richey
*/
class AnyBase {
private $charset;
private $base;
function __construct($charset="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_~") {
$this->charset = $charset;
$this->base = strlen($charset);
}
function encode($n) {
$converted = "";
while ($n > 0) {
$converted = substr($this->charset, ($n % $this->base), 1) . $converted;
$n = floor($n / $this->base);
}
return $converted;
}
function decode($n) {
$c = 0;
for ($i = strlen($n); $i; $i--) {
$c += strpos($this->charset, substr($n, (-1 * ( $i - strlen($n) )), 1)) * pow($this->base, $i - 1);
}
return $c;
}
}