Project

General

Profile

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
	function Path($head, $tail)
29
	{
30
		$this->head = $head;
31
		$this->tail = $tail;
32
	}
33
}
34
35
function partition($str, $delim)
36
{
37 7896 aaronmk
	$delim_idx = strpos($delim, $str);
38 7897 aaronmk
	return $delim_idx !== false ? new Path(substr($str, 0, $delim_idx),
39 7876 aaronmk
		substr($str, $delim_idx+strlen($delim))) : new Path($str, "");
40
}
41
42
function parse_dot_path($path) { return partition($path, "."); }
43
44
function parse_mixed_path($path)
45
{
46
	preg_match('/^([\w-]*)(.*)$/', $path, $path);
47
	return new Path($path[1], rm_prefix(".", $path[2]));
48
}
49
50
function strip_url($url) { return rm_suffix("#", $url); }
51
?>