This repository was archived by the owner on May 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpairtree.class.php
More file actions
48 lines (39 loc) · 1.37 KB
/
pairtree.class.php
File metadata and controls
48 lines (39 loc) · 1.37 KB
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
<?php
class Pairtree {
public function encode($identifier) {
$encode_regex = "/[\"*+,<=>?\\\^|]|[^\x21-\x7e]/";
$escaped_string = preg_replace_callback($encode_regex, 'Pairtree::strtohex', $identifier);
$escaped_string = str_replace(array('/',':','.'), array('=','+',','),$escaped_string);
return $escaped_string;
}
public function decode($identifier) {
$decode_regex = "/\\^(..)/";
$decoded_string = str_replace(array('=','+',','), array('/',':','.'), $identifier);
$decoded_string = preg_replace_callback($decode_regex, 'Pairtree::hextostr', $decoded_string);
return $decoded_string;
}
private function hextostr($matches) {
$s= '';
$x = trim($matches[0],'^');
foreach(explode("\n",trim(chunk_split($x,2))) as $h) $s.=chr(hexdec($h));
return($s);
}
private function strtohex($matches) {
$s= '';
$x= $matches[0];
foreach(str_split($x) as $c) $s.= '^'.sprintf("%02x",ord($c));
return($s);
}
public function id_to_path($identifier) {
$encoded_identifier = self::encode($identifier);
$number = preg_match_all('/..?/',$encoded_identifier,$matches);
$path = implode('/', $matches[0]);
return $path;
}
public function path_to_id($path) {
$encoded_identifier = implode('',explode('/',$path));
$identifier = self::decode($encoded_identifier);
return $identifier;
}
}
?>