1 |
7876
|
aaronmk
|
<?php
|
2 |
|
|
function starts_with($str, $prefix)
|
3 |
|
|
{
|
4 |
|
|
return substr($str, 0, strlen($prefix)) === $prefix;
|
5 |
|
|
}
|
6 |
|
|
|
7 |
|
|
function ends_with($str, $suffix)
|
8 |
|
|
{
|
9 |
|
|
return substr($str, -strlen($suffix)) === $suffix;
|
10 |
|
|
}
|
11 |
|
|
|
12 |
|
|
function rm_prefix($prefix, $str)
|
13 |
|
|
{
|
14 |
|
|
return starts_with($str, $prefix) ? substr($str, strlen($prefix)) : $str;
|
15 |
|
|
}
|
16 |
|
|
|
17 |
|
|
function rm_suffix($suffix, $str)
|
18 |
|
|
{
|
19 |
|
|
return ends_with($str, $suffix) ?
|
20 |
|
|
substr($str, 0, strlen($str) - strlen($suffix)) : $str;
|
21 |
|
|
}
|
22 |
|
|
|
23 |
|
|
class Path
|
24 |
|
|
{
|
25 |
|
|
public $head;
|
26 |
|
|
public $tail;
|
27 |
|
|
|
28 |
7901
|
aaronmk
|
function Path($head, $tail="")
|
29 |
7876
|
aaronmk
|
{
|
30 |
|
|
$this->head = $head;
|
31 |
|
|
$this->tail = $tail;
|
32 |
|
|
}
|
33 |
|
|
}
|
34 |
|
|
|
35 |
|
|
function partition($str, $delim)
|
36 |
|
|
{
|
37 |
7898
|
aaronmk
|
$delim_idx = strpos($str, $delim);
|
38 |
7897
|
aaronmk
|
return $delim_idx !== false ? new Path(substr($str, 0, $delim_idx),
|
39 |
7901
|
aaronmk
|
substr($str, $delim_idx+strlen($delim))) : new Path($str);
|
40 |
7876
|
aaronmk
|
}
|
41 |
|
|
|
42 |
7908
|
aaronmk
|
function array_non_empty($array)
|
43 |
|
|
{
|
44 |
|
|
foreach ($array as $key => $value)
|
45 |
|
|
if (!isset($value) || $value === "") unset($array[$key]);
|
46 |
|
|
return $array;
|
47 |
|
|
}
|
48 |
|
|
|
49 |
|
|
function join_non_empty($delim, $array)
|
50 |
|
|
{
|
51 |
|
|
return implode($delim, array_non_empty($array));
|
52 |
|
|
}
|
53 |
|
|
|
54 |
7876
|
aaronmk
|
function parse_dot_path($path) { return partition($path, "."); }
|
55 |
|
|
|
56 |
|
|
function parse_mixed_path($path)
|
57 |
|
|
{
|
58 |
|
|
preg_match('/^([\w-]*)(.*)$/', $path, $path);
|
59 |
|
|
return new Path($path[1], rm_prefix(".", $path[2]));
|
60 |
|
|
}
|
61 |
|
|
|
62 |
|
|
function strip_url($url) { return rm_suffix("#", $url); }
|
63 |
|
|
?>
|