Project

General

Profile

1
<?php
2
function coalesce($value, $empty) { return isset($value) ? $value : $empty; }
3

    
4
function starts_with($str, $prefix)
5
{
6
	return substr($str, 0, strlen($prefix)) === $prefix;
7
}
8

    
9
function ends_with($str, $suffix)
10
{
11
	return substr($str, -strlen($suffix)) === $suffix;
12
}
13

    
14
function rm_prefix($prefix, $str)
15
{
16
	return starts_with($str, $prefix) ? substr($str, strlen($prefix)) : $str;
17
}
18

    
19
function rm_suffix($suffix, $str)
20
{
21
	return ends_with($str, $suffix) ?
22
		substr($str, 0, strlen($str) - strlen($suffix)) : $str;
23
}
24

    
25
function ensure_prefix($prefix, $str)
26
{
27
	return starts_with($str, $prefix) ? $str : $prefix.$str;
28
}
29

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

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

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

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

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

    
66
function parse_dot_path($path) { return partition($path, "."); }
67

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

    
74
function strip_url($url) { return rm_suffix("#", $url); }
75
?>
(26-26/26)