str_remove_accents()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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