Project

General

Profile

1
<?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
function ensure_prefix($prefix, $str)
24
{
25
	return starts_with($str, $prefix) ? $str : $prefix.$str;
26
}
27

    
28
function ensure_suffix($suffix, $str)
29
{
30
	return ends_with($str, $suffix) ? $str : $str.$suffix;
31
}
32

    
33
class Path
34
{
35
	public $head;
36
	public $tail;
37
	
38
	function Path($head, $tail="")
39
	{
40
		$this->head = $head;
41
		$this->tail = $tail;
42
	}
43
}
44

    
45
function partition($str, $delim)
46
{
47
	$delim_idx = strpos($str, $delim);
48
	return $delim_idx !== false ? new Path(substr($str, 0, $delim_idx),
49
		substr($str, $delim_idx+strlen($delim))) : new Path($str);
50
}
51

    
52
function array_non_empty($array)
53
{
54
	foreach ($array as $key => $value)
55
		if (!isset($value) || $value === "") unset($array[$key]);
56
	return $array;
57
}
58

    
59
function join_non_empty($delim, $array)
60
{
61
	return implode($delim, array_non_empty($array));
62
}
63

    
64
function parse_dot_path($path) { return partition($path, "."); }
65

    
66
function parse_mixed_path($path)
67
{
68
	preg_match('/^([\w-]*)(.*)$/', $path, $path);
69
	return new Path($path[1], rm_prefix(".", $path[2]));
70
}
71

    
72
function strip_url($url) { return rm_suffix("#", $url); }
73
?>
(17-17/17)