-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
58 lines (53 loc) · 1.44 KB
/
functions.php
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
49
50
51
52
53
54
55
56
57
58
<?php
/**
* Converts path slashes to unix format, working on win platform too.
* @param string $path
* @return string
*/
function unixPath(string $path): string {
return str_replace('\\', '/', $path);
}
/**
* Returns true if $substring is at the beginning of source string.
* @param string $str
* @param string $substring
* @return bool
*/
function startsWith(string $str, string $substring): bool {
return (strpos($str, $substring) === 0);
}
/**
* Returns true if $substring is at the end of source string.
* @param string $str
* @param string $substring
* @return bool
*/
function endsWith(string $str, string $substring): bool {
return (strrpos($str, $substring) === strlen($str) - strlen($substring));
}
/**
* Removes substring from the beginning of the source string, if it is found.
* @param string $str
* @param string $substring
* @return string
*/
function trimLeft(string $str, string $substring): string {
if ($substring !== '') {
if (startsWith($str, $substring)) $str = substr($str, strlen($substring));
if ($str === false) $str = '';
}
return $str;
}
/**
* Removes substring from the end of the source string, if it is found.
* @param string $str
* @param string $substring
* @return string
*/
function trimRight(string $str, string $substring): string {
if ($substring !== '') {
if (endsWith($str, $substring)) $str = substr($str, 0, strlen($str) - strlen($substring));
if ($str === false) $str = '';
}
return $str;
}