-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshortening_funcs.php
More file actions
34 lines (30 loc) · 880 Bytes
/
shortening_funcs.php
File metadata and controls
34 lines (30 loc) · 880 Bytes
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
<?php
$alphabet = array('q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'z', 'x', 'c', 'v', 'b', 'n', 'm',
'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X', 'C', 'V', 'B', 'N', 'M',
'2', '3', '4', '5', '6', '7', '8', '9');
/*
* Encode an id (integer) into a url code
*/
function encode($id, $alphabet) {
$code = "";
$base = count($alphabet);
while ($id != 0) {
$char = $alphabet[$id % $base];
$code = $char . $code;
$id = (int)($id/$base);
}
return $code;
}
/*
* Decode a url code into ann id (integer)
*/
function decode($code, $alphabet) {
$id = 0;
$len = strlen($code);
$base = count($alphabet);
for ($i = 0; $i < $len; $i++) {
$id += pow($base, $i)*array_search($code{$len-1-$i}, $alphabet);
}
return $id;
}
?>