Completed
Branch master (f8a0b6)
by Arnold
06:06
created

string_functions.php ➔ str_remove_accents()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 40
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 34
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 40
ccs 31
cts 31
cp 1
crap 1
rs 8.8571
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
 * Replace characters with accents with normal characters.
43
 *
44
 * @param string $string
45
 * @return string
46
 */
47
function str_remove_accents($string)
48
{
49
    $from = [
50 2
        'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î',
51 2
        'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß',
52 2
        'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î',
53 2
        'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'Ā',
54 2
        'ā', 'Ă', 'ă', 'Ą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', 'A', 'A', 'A', 'A', 'AE', 'C', 'E', 'E', 'E', 'E', 'I', 'I', 'I',
69 2
        'I', 'D', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U', 'U', 'Y', 's',
70 2
        'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i',
71 2
        'i', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y', 'A',
72 2
        'a', 'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd',
73 2
        'D', 'd', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G',
74 2
        'g', 'G', 'g', 'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i',
75 2
        'I', 'i', 'I', 'i', 'IJ', 'ij', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L',
76 2
        'l', 'L', 'l', 'l', 'l', 'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O',
77 2
        'o', 'O', 'o', 'OE', 'oe', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's',
78 2
        'S', 's', 'S', 's', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U',
79 2
        'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Y', 'Z', 'z', 'Z',
80 2
        'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o',
81 2
        'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O',
82
        'o'
83 2
    ];
84
    
85 2
    return str_replace($from, $to, $string);
86
}
87
88
/**
89
 * Generate a URL friendly slug from the given string
90
 *
91
 * @param string $string
92
 * @return string
93
 */
94
function str_slug($string, $glue = '-')
95
{
96 1
    $normalized = str_remove_accents($string);
97 1
    $lower = strtolower($normalized);
98 1
    return preg_replace('/[\W_]+/', $glue, $lower);
99
}
100