Completed
Push — master ( fc8efb...ee53c9 )
by Arnold
02:26
created

string_functions.php ➔ str_after()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 5
ccs 2
cts 2
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Jasny;
4
5
/**
6
 * Check if a string starts with a substring
7
 *
8
 * @param string $string
9
 * @param string $substr
10
 * @return boolean
11
 */
12
function str_starts_with($string, $substr)
13
{
14 1
    return strpos($string, $substr) === 0;
15
}
16
17
/**
18
 * Check if a string ends with a substring
19
 *
20
 * @param string $string
21
 * @param string $substr
22
 * @return boolean
23
 */
24
function str_ends_with($string, $substr)
25
{
26 1
    return stripos($string, $substr) === strlen($string) - strlen($substr);
27
}
28
29
/**
30
 * Check if a string contains a substring
31
 *
32
 * @param string $string
33
 * @param string $substr
34
 * @return boolean
35
 */
36
function str_contains($string, $substr)
37
{
38 1
    return strpos($string, $substr) !== false;
39
}
40
41
/**
42
 * Get the string before the first occurence of the substring.
43
 * If the substring is not found, the whole string is returned.
44
 *
45
 * @param string $string
46
 * @param string $substr
47
 * @return string
48
 */
49
function str_before($string, $substr)
50
{
51 5
    $pos = strpos($string, $substr);
52 5
    return $pos === false ? $string : substr($string, 0, $pos);
53
}
54
55
/**
56
 * Get the string after the first occurence of the substring.
57
 * If the substring is not found, an empty string is returned.
58
 *
59
 * @param string $string
60
 * @param string $substr
61
 * @return string
62
 */
63
function str_after($string, $substr)
64
{
65 6
    $pos = strpos($string, $substr);
66 6
    return $pos === false ? '' : substr($string, $pos + strlen($substr));
67
}
68
69
/**
70
 * Replace characters with accents with normal characters.
71
 *
72
 * @param string $string
73
 * @return string
74
 */
75
function str_remove_accents($string)
76
{
77
    $from = [
78 1
        'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î',
79 1
        'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß',
80 1
        'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î',
81 1
        'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'Ā',
82 1
        'ā', 'Ă', 'ă', 'Ą', 'ą', 'Ć', 'ć', 'Ĉ', 'ĉ', 'Ċ', 'ċ', 'Č', 'č', 'Ď', 'ď',
83 1
        'Đ', 'đ', 'Ē', 'ē', 'Ĕ', 'ĕ', 'Ė', 'ė', 'Ę', 'ę', 'Ě', 'ě', 'Ĝ', 'ĝ', 'Ğ',
84 1
        'ğ', 'Ġ', 'ġ', 'Ģ', 'ģ', 'Ĥ', 'ĥ', 'Ħ', 'ħ', 'Ĩ', 'ĩ', 'Ī', 'ī', 'Ĭ', 'ĭ',
85 1
        'Į', 'į', 'İ', 'ı', 'IJ', 'ij', 'Ĵ', 'ĵ', 'Ķ', 'ķ', 'Ĺ', 'ĺ', 'Ļ', 'ļ', 'Ľ',
86 1
        'ľ', 'Ŀ', 'ŀ', 'Ł', 'ł', 'Ń', 'ń', 'Ņ', 'ņ', 'Ň', 'ň', 'ʼn', 'Ō', 'ō', 'Ŏ',
87 1
        'ŏ', 'Ő', 'ő', 'Œ', 'œ', 'Ŕ', 'ŕ', 'Ŗ', 'ŗ', 'Ř', 'ř', 'Ś', 'ś', 'Ŝ', 'ŝ',
88 1
        'Ş', 'ş', 'Š', 'š', 'Ţ', 'ţ', 'Ť', 'ť', 'Ŧ', 'ŧ', 'Ũ', 'ũ', 'Ū', 'ū', 'Ŭ',
89 1
        'ŭ', 'Ů', 'ů', 'Ű', 'ű', 'Ų', 'ų', 'Ŵ', 'ŵ', 'Ŷ', 'ŷ', 'Ÿ', 'Ź', 'ź', 'Ż',
90 1
        'ż', 'Ž', 'ž', 'ſ', 'ƒ', 'Ơ', 'ơ', 'Ư', 'ư', 'Ǎ', 'ǎ', 'Ǐ', 'ǐ', 'Ǒ', 'ǒ',
91 1
        'Ǔ', 'ǔ', 'Ǖ', 'ǖ', 'Ǘ', 'ǘ', 'Ǚ', 'ǚ', 'Ǜ', 'ǜ', 'Ǻ', 'ǻ', 'Ǽ', 'ǽ', 'Ǿ',
92
        'ǿ'
93 1
    ];
94
      
95
    $to = [
96 1
        'A', 'A', 'A', 'A', 'A', 'A', 'AE', 'C', 'E', 'E', 'E', 'E', 'I', 'I', 'I',
97 1
        'I', 'D', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U', 'U', 'Y', 's',
98 1
        'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i',
99 1
        'i', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y', 'A',
100 1
        'a', 'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd',
101 1
        'D', 'd', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G',
102 1
        'g', 'G', 'g', 'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i',
103 1
        'I', 'i', 'I', 'i', 'IJ', 'ij', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L',
104 1
        'l', 'L', 'l', 'l', 'l', 'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O',
105 1
        'o', 'O', 'o', 'OE', 'oe', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's',
106 1
        'S', 's', 'S', 's', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U',
107 1
        'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Y', 'Z', 'z', 'Z',
108 1
        'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o',
109 1
        'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O',
110
        'o'
111 1
    ];
112
    
113 1
    return str_replace($from, $to, $string);
114
}
115
116
/**
117
 * Generate a URL friendly slug from the given string
118
 *
119
 * @param string $string
120
 * @return string
121
 */
122
function str_slug($string, $glue = '-')
123
{
124 1
    $normalized = str_remove_accents($string);
125 1
    $lower = strtolower($normalized);
126 1
    return preg_replace('/[\W_]+/', $glue, $lower);
127
}
128
129