-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathShortURL.php
50 lines (40 loc) · 1.17 KB
/
ShortURL.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
/*
* ShortURL (https://github.com/delight-im/ShortURL)
* Copyright (c) delight.im (https://www.delight.im/)
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
*/
/**
* ShortURL: Bijective conversion between natural numbers (IDs) and short strings
*
* ShortURL::encode() takes an ID and turns it into a short string
* ShortURL::decode() takes a short string and turns it into an ID
*
* Features:
* + large alphabet (51 chars) and thus very short resulting strings
* + proof against offensive words (removed 'a', 'e', 'i', 'o' and 'u')
* + unambiguous (removed 'I', 'l', '1', 'O' and '0')
*
* Example output:
* 123456789 <=> pgK8p
*/
class ShortURL {
const ALPHABET = '23456789bcdfghjkmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ-_';
const BASE = 51; // strlen(self::ALPHABET)
public static function encode($num) {
$str = '';
while ($num > 0) {
$str = self::ALPHABET[($num % self::BASE)] . $str;
$num = (int) ($num / self::BASE);
}
return $str;
}
public static function decode($str) {
$num = 0;
$len = strlen($str);
for ($i = 0; $i < $len; $i++) {
$num = $num * self::BASE + strpos(self::ALPHABET, $str[$i]);
}
return $num;
}
}