Completed
Push — master ( d62687...0c8a8b )
by Aurimas
8s
created

Strings.php ➔ camelize()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 21
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 34
ccs 19
cts 19
cp 1
crap 3
rs 8.8571
1
<?php
2
3
namespace Funct\Strings;
4
5
/**
6
 * Extracts the string between two substrings
7
 *
8
 * @param string $input
9
 * @param string $left
10
 * @param string $right
11
 *
12
 * @return string
13
 *
14
 * @author Lucantis Swann <[email protected]>
15
 */
16
function between($input, $left, $right)
17
{
18 4
    $input = ' ' . $input;
19 4
    $ini   = strpos($input, $left);
20
21 4
    if ($ini == 0) {
22 1
        return '';
23
    }
24
25 3
    $ini += strlen($left);
26 3
    $len = strpos($input, $right, $ini) - $ini;
27
28 3
    return substr($input, $ini, $len);
29
}
30
31
32
/**
33
 * Camelizes string
34
 *
35
 * @param string $input
36
 * @param bool   $firstLetterUppercase
37
 *
38
 * @return string
39
 *
40
 * @author Aurimas Niekis <[email protected]>
41
 */
42
function camelize($input, $firstLetterUppercase = false)
43
{
44 24
    $input = trim($input);
45
46 24
    if ($firstLetterUppercase) {
47 16
        $input = upperCaseFirst($input);
48 16
    } else {
49 8
        $input = lowerCaseFirst($input);
50
    }
51
52 24
    $input = preg_replace('/^[-_]+/', '', $input);
53
54 24
    $input = preg_replace_callback(
55 24
        '/[-_\s]+(.)?/u',
56
        function ($match) {
57 15
            if (isset($match[1])) {
58 9
                return strtoupper($match[1]);
59
            } else {
60 6
                return '';
61
            }
62 24
        },
63
        $input
64 24
    );
65
66 24
    $input = preg_replace_callback(
67 24
        '/[\d]+(.)?/u',
68
        function ($match) {
69 3
            return strtoupper($match[0]);
70 24
        },
71
        $input
72 24
    );
73
74 24
    return $input;
75
}
76
77
78
/**
79
 * Removes prefix from start of string
80
 *
81
 * @param string $input
82
 * @param string $prefix
83
 *
84
 * @return string
85
 *
86
 * @author Lucantis Swann <[email protected]>
87
 */
88
function chompLeft($input, $prefix)
89
{
90 4
    if (startsWith($input, $prefix)) {
91 2
        return mb_substr($input, mb_strlen($prefix));
92
    }
93
94 2
    return $input;
95
}
96
97
/**
98
 * Removes suffix from end of string
99
 *
100
 * @param string $input
101
 * @param string $suffix
102
 *
103
 * @return string
104
 *
105
 * @author Lucantis Swann <[email protected]>
106
 */
107
function chompRight($input, $suffix)
108
{
109 4
    if (endsWith($input, $suffix)) {
110
111 2
        return mb_substr($input, 0, mb_strlen($input) - mb_strlen($suffix));
112
    }
113
114 2
    return $input;
115
}
116
117
/**
118
 * Converts string to camelized class name. First letter is always upper case
119
 *
120
 * @param string $string
121
 *
122
 * @return string
123
 * @author Aurimas Niekis <[email protected]>
124
 */
125
function classify($string)
126
{
127 8
    return camelize($string, true);
128
}
129
130
131
/**
132
 * Collapse multiple spaces
133
 *
134
 * @param string $input
135
 *
136
 * @return string
137
 *
138
 * @author Lucantis Swann <[email protected]>
139
 */
140
function collapseWhitespace($input)
141
{
142 3
    return preg_replace('/\s+/u', ' ', $input);
143
}
144
145
/**
146
 * Check if string contains substring
147
 *
148
 * @param string $input
149
 * @param string $substring
150
 *
151
 * @return bool
152
 *
153
 * @author Lucantis Swann <[email protected]>
154
 */
155
function contains($input, $substring)
156
{
157 9
    return mb_strpos($input, $substring) !== false;
158
}
159
160
/**
161
 * Count the occurrences of substring in string
162
 *
163
 * @param string $input
164
 * @param string $substring
165
 *
166
 * @return int
167
 *
168
 * @author Lucantis Swann <[email protected]>
169
 */
170
function countOccurrences($input, $substring)
171
{
172 4
    return mb_substr_count($input, $substring);
173
}
174
175
176
/**
177
 * Converts hyphens and camel casing to underscores
178
 *
179
 * @param string $string
180
 *
181
 * @return string
182
 * @author Aurimas Niekis <[email protected]>
183
 */
184
function dasherize($string)
185
{
186 2
    return strtolower(preg_replace('/(?<!^)([A-Z])/', '-$1', str_replace('_', '-', $string)));
187
}
188
189
190
/**
191
 * Check if string ends with substring
192
 *
193
 * @param string $input
194
 * @param string $substring
195
 *
196
 * @return bool
197
 *
198
 * @author Lucantis Swann <[email protected]>
199
 */
200
function endsWith($input, $substring)
201
{
202 8
    return mb_substr($input, -strlen($substring)) === $substring;
203
}
204
205
/**
206
 * Alias of contains
207
 *
208
 * @param string $input
209
 * @param string $substring
210
 *
211
 * @return bool
212
 *
213
 * @author Lucantis Swann <[email protected]>
214
 */
215
function includes($input, $substring)
216
{
217 5
    return contains($input, $substring);
218
}
219
220
221
/**
222
 * Check if string contains only letters
223
 *
224
 * @param string $input
225
 *
226
 * @return bool
227
 *
228
 * @author Lucantis Swann <[email protected]>
229
 */
230
function isAlpha($input)
231
{
232 7
    return ctype_alpha($input);
233
}
234
235
/**
236
 * Check if string contains only alphanumeric
237
 *
238
 * @param string $input
239
 *
240
 * @return bool
241
 *
242
 * @author Lucantis Swann <[email protected]>
243
 */
244
function isAlphaNumeric($input)
245
{
246 7
    return ctype_alnum($input);
247
}
248
249
/**
250
 * Checks if letters in given string are all lowercase.
251
 *
252
 * @param string $input
253
 * @param bool   $mb
254
 *
255
 * @return bool
256
 *
257
 * @author Ernestas Kvedaras <[email protected]>
258
 */
259
function isLower($input, $mb = false)
260
{
261
    return $mb
262 28
        ? mb_strtolower($input, mb_detect_encoding($input, 'auto')) === $input
263 28
        : strtolower($input) === $input;
264
}
265
266
267
/**
268
 * Check if string contains only digits
269
 *
270
 * @param string $input
271
 *
272
 * @return bool
273
 *
274
 * @author Lucantis Swann <[email protected]>
275
 */
276
function isNumeric($input)
277
{
278 7
    return ctype_digit($input);
279
}
280
281
/**
282
 * Checks if letters in given string are all uppercase.
283
 *
284
 * @param string $input
285
 * @param bool   $mb
286
 *
287
 * @return bool
288
 *
289
 * @author Ernestas Kvedaras <[email protected]>
290
 */
291
function isUpper($input, $mb = false)
292
{
293
    return $mb
294 20
        ? mb_strtoupper($input, mb_detect_encoding($input, 'auto')) === $input
295 20
        : strtoupper($input) === $input;
296
}
297
298
299
/**
300
 * Remove accents from latin characters
301
 *
302
 * @param string $input
303
 *
304
 * @return string
305
 *
306
 * @author Lucantis Swann <[email protected]>
307
 */
308
function latinize($input)
309
{
310
    $table = [
311 5
        'أ' => 'a', 'ب' => 'b', 'ت' => 't', 'ث' => 'th', 'ج' => 'g', 'ح' => 'h', 'خ' => 'kh', 'د' => 'd', 'ذ' => 'th',
312 5
        'ر' => 'r', 'ز' => 'z', 'س' => 's', 'ش' => 'sh', 'ص' => 's', 'ض' => 'd', 'ط' => 't', 'ظ' => 'th', 'ع' => 'aa',
313 5
        'غ' => 'gh', 'ف' => 'f', 'ق' => 'k', 'ك' => 'k', 'ل' => 'l', 'م' => 'm', 'ن' => 'n', 'ه' => 'h', 'و' => 'o',
314 5
        'ي' => 'y', 'Ä' => 'A', 'Ö' => 'O', 'Ü' => 'U', 'ß' => 'ss', 'ä' => 'a', 'ö' => 'o', 'ü' => 'u', 'က' => 'k',
315 5
        'ခ' => 'kh', 'ဂ' => 'g', 'ဃ' => 'ga', 'င' => 'ng', 'စ' => 's', 'ဆ' => 'sa', 'ဇ' => 'z', 'စျ' => 'za',
316 5
        'ည' => 'ny', 'ဋ' => 't', 'ဌ' => 'ta', 'ဍ' => 'd', 'ဎ' => 'da', 'ဏ' => 'na', 'တ' => 't', 'ထ' => 'ta', 'ဒ' => 'd',
317 5
        'ဓ' => 'da', 'န' => 'n', 'ပ' => 'p', 'ဖ' => 'pa', 'ဗ' => 'b', 'ဘ' => 'ba', 'မ' => 'm', 'ယ' => 'y', 'ရ' => 'ya',
318 5
        'လ' => 'l', 'ဝ' => 'w', 'သ' => 'th', 'ဟ' => 'h', 'ဠ' => 'la', 'အ' => 'a', 'ြ' => 'y', 'ျ' => 'ya', 'ွ' => 'w',
319 5
        'ြွ' => 'yw', 'ျွ' => 'ywa', 'ှ' => 'h', 'ဧ' => 'e', '၏' => '-e', 'ဣ' => 'i', 'ဤ' => '-i', 'ဉ' => 'u',
320 5
        'ဦ' => '-u', 'ဩ' => 'aw', 'သြော' => 'aw', 'ဪ' => 'aw', '၍' => 'ywae', '၌' => 'hnaik', '၀' => '0', '၁' => '1',
321 5
        '၂' => '2', '၃' => '3', '၄' => '4', '၅' => '5', '၆' => '6', '၇' => '7', '၈' => '8', '၉' => '9', '္' => '',
322 5
        '့' => '', 'း' => '', 'ာ' => 'a', 'ါ' => 'a', 'ေ' => 'e', 'ဲ' => 'e', 'ိ' => 'i', 'ီ' => 'i', 'ို' => 'o',
323 5
        'ု' => 'u', 'ူ' => 'u', 'ေါင်' => 'aung', 'ော' => 'aw', 'ော်' => 'aw', 'ေါ' => 'aw', 'ေါ်' => 'aw', '်' => 'at',
324 5
        'က်' => 'et', 'ိုက်' => 'aik', 'ောက်' => 'auk', 'င်' => 'in', 'ိုင်' => 'aing', 'ောင်' => 'aung', 'စ်' => 'it',
325 5
        'ည်' => 'i', 'တ်' => 'at', 'ိတ်' => 'eik', 'ုတ်' => 'ok', 'ွတ်' => 'ut', 'ေတ်' => 'it', 'ဒ်' => 'd',
326 5
        'ိုဒ်' => 'ok', 'ုဒ်' => 'ait', 'န်' => 'an', 'ာန်' => 'an', 'ိန်' => 'ein', 'ုန်' => 'on', 'ွန်' => 'un',
327 5
        'ပ်' => 'at', 'ိပ်' => 'eik', 'ုပ်' => 'ok', 'ွပ်' => 'ut', 'န်ုပ်' => 'nub', 'မ်' => 'an', 'ိမ်' => 'ein',
328 5
        'ုမ်' => 'on', 'ွမ်' => 'un', 'ယ်' => 'e', 'ိုလ်' => 'ol', 'ဉ်' => 'in', 'ံ' => 'an', 'ိံ' => 'ein',
329 5
        'ုံ' => 'on', 'Č' => 'C', 'Ď' => 'D', 'Ě' => 'E', 'Ň' => 'N', 'Ř' => 'R', 'Š' => 'S', 'Ť' => 'T', 'Ů' => 'U',
330 5
        'Ž' => 'Z', 'č' => 'c', 'ď' => 'd', 'ě' => 'e', 'ň' => 'n', 'ř' => 'r', 'š' => 's', 'ť' => 't', 'ů' => 'u',
331 5
        'ž' => 'z', '°' => 0, '¹' => 1, '²' => 2, '³' => 3, '⁴' => 4, '⁵' => 5, '⁶' => 6, '⁷' => 7, '⁸' => 8, '⁹' => 9,
332 5
        '₀' => 0, '₁' => 1, '₂' => 2, '₃' => 3, '₄' => 4, '₅' => 5, '₆' => 6, '₇' => 7, '₈' => 8, '₉' => 9, 'æ' => 'ae',
333 5
        'ǽ' => 'ae', 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Å' => 'AA', 'Ǻ' => 'A', 'Ă' => 'A', 'Ǎ' => 'A',
334 5
        'Æ' => 'AE', 'Ǽ' => 'AE', 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'å' => 'aa', 'ǻ' => 'a', 'ă' => 'a',
335 5
        'ǎ' => 'a', 'ª' => 'a', '@' => 'at', 'Ĉ' => 'C', 'Ċ' => 'C', 'ĉ' => 'c', 'ċ' => 'c', '©' => 'c', 'Ð' => 'Dj',
336 5
        'Đ' => 'D', 'ð' => 'dj', 'đ' => 'd', 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ĕ' => 'E', 'Ė' => 'E',
337 5
        'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e', 'ĕ' => 'e', 'ė' => 'e', 'ƒ' => 'f', 'Ĝ' => 'G', 'Ġ' => 'G',
338 5
        'ĝ' => 'g', 'ġ' => 'g', 'Ĥ' => 'H', 'Ħ' => 'H', 'ĥ' => 'h', 'ħ' => 'h', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I',
339 5
        'Ï' => 'I', 'Ĩ' => 'I', 'Ĭ' => 'I', 'Ǐ' => 'I', 'Į' => 'I', 'IJ' => 'IJ', 'ì' => 'i', 'í' => 'i', 'î' => 'i',
340 5
        'ï' => 'i', 'ĩ' => 'i', 'ĭ' => 'i', 'ǐ' => 'i', 'į' => 'i', 'ij' => 'ij', 'Ĵ' => 'J', 'ĵ' => 'j', 'Ĺ' => 'L',
341 5
        'Ľ' => 'L', 'Ŀ' => 'L', 'ĺ' => 'l', 'ľ' => 'l', 'ŀ' => 'l', 'Ñ' => 'N', 'ñ' => 'n', 'ʼn' => 'n', 'Ò' => 'O',
342 5
        'Ô' => 'O', 'Õ' => 'O', 'Ō' => 'O', 'Ŏ' => 'O', 'Ǒ' => 'O', 'Ő' => 'O', 'Ơ' => 'O', 'Ø' => 'OE', 'Ǿ' => 'O',
343 5
        'Œ' => 'OE', 'ò' => 'o', 'ô' => 'o', 'õ' => 'o', 'ō' => 'o', 'ŏ' => 'o', 'ǒ' => 'o', 'ő' => 'o', 'ơ' => 'o',
344 5
        'ø' => 'oe', 'ǿ' => 'o', 'º' => 'o', 'œ' => 'oe', 'Ŕ' => 'R', 'Ŗ' => 'R', 'ŕ' => 'r', 'ŗ' => 'r', 'Ŝ' => 'S',
345 5
        'Ș' => 'S', 'ŝ' => 's', 'ș' => 's', 'ſ' => 's', 'Ţ' => 'T', 'Ț' => 'T', 'Ŧ' => 'T', 'Þ' => 'TH', 'ţ' => 't',
346 5
        'ț' => 't', 'ŧ' => 't', 'þ' => 'th', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ũ' => 'U', 'Ŭ' => 'U', 'Ű' => 'U',
347 5
        'Ų' => 'U', 'Ư' => 'U', 'Ǔ' => 'U', 'Ǖ' => 'U', 'Ǘ' => 'U', 'Ǚ' => 'U', 'Ǜ' => 'U', 'ù' => 'u', 'ú' => 'u',
348 5
        'û' => 'u', 'ũ' => 'u', 'ŭ' => 'u', 'ű' => 'u', 'ų' => 'u', 'ư' => 'u', 'ǔ' => 'u', 'ǖ' => 'u', 'ǘ' => 'u',
349 5
        'ǚ' => 'u', 'ǜ' => 'u', 'Ŵ' => 'W', 'ŵ' => 'w', 'Ý' => 'Y', 'Ÿ' => 'Y', 'Ŷ' => 'Y', 'ý' => 'y', 'ÿ' => 'y',
350 5
        'ŷ' => 'y', 'ა' => 'a', 'ბ' => 'b', 'გ' => 'g', 'დ' => 'd', 'ე' => 'e', 'ვ' => 'v', 'ზ' => 'z', 'თ' => 't',
351 5
        'ი' => 'i', 'კ' => 'k', 'ლ' => 'l', 'მ' => 'm', 'ნ' => 'n', 'ო' => 'o', 'პ' => 'p', 'ჟ' => 'zh', 'რ' => 'r',
352 5
        'ს' => 's', 'ტ' => 't', 'უ' => 'u', 'ფ' => 'f', 'ქ' => 'k', 'ღ' => 'gh', 'ყ' => 'q', 'შ' => 'sh', 'ჩ' => 'ch',
353 5
        'ც' => 'ts', 'ძ' => 'dz', 'წ' => 'ts', 'ჭ' => 'ch', 'ხ' => 'kh', 'ჯ' => 'j', 'ჰ' => 'h', 'ΑΥ' => 'AU',
354 5
        'Αυ' => 'Au', 'ΟΥ' => 'OU', 'Ου' => 'Ou', 'ΕΥ' => 'EU', 'Ευ' => 'Eu', 'ΕΙ' => 'I', 'Ει' => 'I', 'ΟΙ' => 'I',
355 5
        'Οι' => 'I', 'ΥΙ' => 'I', 'Υι' => 'I', 'ΑΎ' => 'AU', 'Αύ' => 'Au', 'ΟΎ' => 'OU', 'Ού' => 'Ou', 'ΕΎ' => 'EU',
356 5
        'Εύ' => 'Eu', 'ΕΊ' => 'I', 'Εί' => 'I', 'ΟΊ' => 'I', 'Οί' => 'I', 'ΎΙ' => 'I', 'Ύι' => 'I', 'ΥΊ' => 'I',
357 5
        'Υί' => 'I', 'αυ' => 'au', 'ου' => 'ou', 'ευ' => 'eu', 'ει' => 'i', 'οι' => 'i', 'υι' => 'i', 'αύ' => 'au',
358 5
        'ού' => 'ou', 'εύ' => 'eu', 'εί' => 'i', 'οί' => 'i', 'ύι' => 'i', 'υί' => 'i', 'Α' => 'A', 'Β' => 'V',
359 5
        'Γ' => 'G', 'Δ' => 'D', 'Ε' => 'E', 'Ζ' => 'Z', 'Η' => 'I', 'Θ' => 'Th', 'Ι' => 'I', 'Κ' => 'K', 'Λ' => 'L',
360 5
        'Μ' => 'M', 'Ν' => 'N', 'Ξ' => 'X', 'Ο' => 'O', 'Π' => 'P', 'Ρ' => 'R', 'Σ' => 'S', 'Τ' => 'T', 'Υ' => 'I',
361 5
        'Φ' => 'F', 'Χ' => 'Ch', 'Ψ' => 'Ps', 'Ω' => 'O', 'Ά' => 'A', 'Έ' => 'E', 'Ή' => 'I', 'Ί' => 'I', 'Ό' => 'O',
362 5
        'Ύ' => 'I', 'Ϊ' => 'I', 'Ϋ' => 'I', 'ϒ' => 'I', 'α' => 'a', 'β' => 'v', 'γ' => 'g', 'δ' => 'd', 'ε' => 'e',
363 5
        'ζ' => 'z', 'η' => 'i', 'θ' => 'th', 'ι' => 'i', 'κ' => 'k', 'λ' => 'l', 'μ' => 'm', 'ν' => 'n', 'ξ' => 'x',
364 5
        'ο' => 'o', 'π' => 'p', 'ρ' => 'r', 'ς' => 's', 'σ' => 's', 'τ' => 't', 'υ' => 'i', 'φ' => 'f', 'χ' => 'ch',
365 5
        'ψ' => 'ps', 'ω' => 'o', 'ά' => 'a', 'έ' => 'e', 'ή' => 'i', 'ί' => 'i', 'ό' => 'o', 'ύ' => 'i', 'ϊ' => 'i',
366 5
        'ϋ' => 'i', 'ΰ' => 'i', 'ώ' => 'o', 'ϐ' => 'v', 'ϑ' => 'th', 'अ' => 'a', 'आ' => 'aa', 'ए' => 'e', 'ई' => 'ii',
367 5
        'ऍ' => 'ei', 'ऎ' => 'ऎ', 'ऐ' => 'ai', 'इ' => 'i', 'ओ' => 'o', 'ऑ' => 'oi', 'ऒ' => 'oii', 'ऊ' => 'uu',
368 5
        'औ' => 'ou', 'उ' => 'u', 'ब' => 'B', 'भ' => 'Bha', 'च' => 'Ca', 'छ' => 'Chha', 'ड' => 'Da', 'ढ' => 'Dha',
369 5
        'फ' => 'Fa', 'फ़' => 'Fi', 'ग' => 'Ga', 'घ' => 'Gha', 'ग़' => 'Ghi', 'ह' => 'Ha', 'ज' => 'Ja', 'झ' => 'Jha',
370 5
        'क' => 'Ka', 'ख' => 'Kha', 'ख़' => 'Khi', 'ल' => 'L', 'ळ' => 'Li', 'ऌ' => 'Li', 'ऴ' => 'Lii', 'ॡ' => 'Lii',
371 5
        'म' => 'Ma', 'न' => 'Na', 'ङ' => 'Na', 'ञ' => 'Nia', 'ण' => 'Nae', 'ऩ' => 'Ni', 'ॐ' => 'oms', 'प' => 'Pa',
372 5
        'क़' => 'Qi', 'र' => 'Ra', 'ऋ' => 'Ri', 'ॠ' => 'Ri', 'ऱ' => 'Ri', 'स' => 'Sa', 'श' => 'Sha', 'ष' => 'Shha',
373 5
        'ट' => 'Ta', 'त' => 'Ta', 'ठ' => 'Tha', 'द' => 'Tha', 'थ' => 'Tha', 'ध' => 'Thha', 'ड़' => 'ugDha',
374 5
        'ढ़' => 'ugDhha', 'व' => 'Va', 'य' => 'Ya', 'य़' => 'Yi', 'ज़' => 'Za', 'Ā' => 'A', 'Ē' => 'E', 'Ģ' => 'G',
375 5
        'Ī' => 'I', 'Ķ' => 'K', 'Ļ' => 'L', 'Ņ' => 'N', 'Ū' => 'U', 'ā' => 'a', 'ē' => 'e', 'ģ' => 'g', 'ī' => 'i',
376 5
        'ķ' => 'k', 'ļ' => 'l', 'ņ' => 'n', 'ū' => 'u', 'Ą' => 'A', 'Ć' => 'C', 'Ę' => 'E', 'Ł' => 'L', 'Ń' => 'N',
377 5
        'Ó' => 'O', 'Ś' => 'S', 'Ź' => 'Z', 'Ż' => 'Z', 'ą' => 'a', 'ć' => 'c', 'ę' => 'e', 'ł' => 'l', 'ń' => 'n',
378 5
        'ó' => 'o', 'ś' => 's', 'ź' => 'z', 'ż' => 'z', 'Ъ' => '', 'Ь' => '', 'А' => 'A', 'Б' => 'B', 'Ц' => 'C',
379 5
        'Ч' => 'Ch', 'Д' => 'D', 'Е' => 'E', 'Ё' => 'E', 'Э' => 'E', 'Ф' => 'F', 'Г' => 'G', 'Х' => 'H', 'И' => 'I',
380 5
        'Й' => 'Y', 'Я' => 'Ya', 'Ю' => 'Yu', 'К' => 'K', 'Л' => 'L', 'М' => 'M', 'Н' => 'N', 'О' => 'O', 'П' => 'P',
381 5
        'Р' => 'R', 'С' => 'S', 'Ш' => 'Sh', 'Щ' => 'Shch', 'Т' => 'T', 'У' => 'U', 'В' => 'V', 'Ы' => 'Y', 'З' => 'Z',
382 5
        'Ж' => 'Zh', 'ъ' => '', 'ь' => '', 'а' => 'a', 'б' => 'b', 'ц' => 'c', 'ч' => 'ch', 'д' => 'd', 'е' => 'e',
383 5
        'ё' => 'e', 'э' => 'e', 'ф' => 'f', 'г' => 'g', 'х' => 'h', 'и' => 'i', 'й' => 'y', 'я' => 'ya', 'ю' => 'yu',
384 5
        'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n', 'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's', 'ш' => 'sh',
385 5
        'щ' => 'shch', 'т' => 't', 'у' => 'u', 'в' => 'v', 'ы' => 'y', 'з' => 'z', 'ж' => 'zh', 'Ç' => 'C', 'Ğ' => 'G',
386 5
        'İ' => 'I', 'Ş' => 'S', 'ç' => 'c', 'ğ' => 'g', 'ı' => 'i', 'ş' => 's', 'Ґ' => 'G', 'І' => 'I', 'Ї' => 'Ji',
387 5
        'Є' => 'Ye', 'ґ' => 'g', 'і' => 'i', 'ї' => 'ji', 'є' => 'ye', 'ạ' => 'a', 'ả' => 'a', 'ầ' => 'a', 'ấ' => 'a',
388 5
        'ậ' => 'a', 'ẩ' => 'a', 'ẫ' => 'a', 'ằ' => 'a', 'ắ' => 'a', 'ặ' => 'a', 'ẳ' => 'a', 'ẵ' => 'a', 'ẹ' => 'e',
389 5
        'ẻ' => 'e', 'ẽ' => 'e', 'ề' => 'e', 'ế' => 'e', 'ệ' => 'e', 'ể' => 'e', 'ễ' => 'e', 'ị' => 'i', 'ỉ' => 'i',
390 5
        'ọ' => 'o', 'ỏ' => 'o', 'ồ' => 'o', 'ố' => 'o', 'ộ' => 'o', 'ổ' => 'o', 'ỗ' => 'o', 'ờ' => 'o', 'ớ' => 'o',
391 5
        'ợ' => 'o', 'ở' => 'o', 'ỡ' => 'o', 'ụ' => 'u', 'ủ' => 'u', 'ừ' => 'u', 'ứ' => 'u', 'ự' => 'u', 'ử' => 'u',
392 5
        'ữ' => 'u', 'ỳ' => 'y', 'ỵ' => 'y', 'ỷ' => 'y', 'ỹ' => 'y', 'Ạ' => 'A', 'Ả' => 'A', 'Ầ' => 'A', 'Ấ' => 'A',
393 5
        'Ậ' => 'A', 'Ẩ' => 'A', 'Ẫ' => 'A', 'Ằ' => 'A', 'Ắ' => 'A', 'Ặ' => 'A', 'Ẳ' => 'A', 'Ẵ' => 'A', 'Ẹ' => 'E',
394 5
        'Ẻ' => 'E', 'Ẽ' => 'E', 'Ề' => 'E', 'Ế' => 'E', 'Ệ' => 'E', 'Ể' => 'E', 'Ễ' => 'E', 'Ị' => 'I', 'Ỉ' => 'I',
395 5
        'Ọ' => 'O', 'Ỏ' => 'O', 'Ồ' => 'O', 'Ố' => 'O', 'Ộ' => 'O', 'Ổ' => 'O', 'Ỗ' => 'O', 'Ờ' => 'O', 'Ớ' => 'O',
396 5
        'Ợ' => 'O', 'Ở' => 'O', 'Ỡ' => 'O', 'Ụ' => 'U', 'Ủ' => 'U', 'Ừ' => 'U', 'Ứ' => 'U', 'Ự' => 'U', 'Ử' => 'U',
397 5
    ];
398
399 5
    $string = strtr($input, $table);
400
401 5
    return $string;
402
}
403
404
405
/**
406
 * Return the substring denoted by n positive left-most characters
407
 *
408
 * @param string $string
409
 * @param int    $n
410
 *
411
 * @return string
412
 * @author Aurimas Niekis <[email protected]>
413
 */
414 View Code Duplication
function left($string, $n)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
415
{
416 3
    $start = 0;
417 3
    if ($n < 0) {
418 1
        $start = $n;
419 1
        $n     = -$n;
420 1
    }
421
422 3
    return substr($string, $start, $n);
423
}
424
425
/**
426
 * Return the length of a string
427
 *
428
 * @param  string $string the input string
429
 * @param bool    $mb     to use or not to use mb_strlen
430
 *
431
 * @return int         the length of the input string
432
 * @author Rod Elias <[email protected]>
433
 */
434
function len($string, $mb = false)
435
{
436
    return length($string, $mb);
437
}
438
439
/**
440
 * Return the length of a string
441
 *
442
 * @param  string $string the input string
443
 * @param bool    $mb     to use or not to use mb_strlen
444
 *
445
 * @return int         the length of the input string
446
 * @author Rod Elias <[email protected]>
447
 */
448
function length($string, $mb = false)
449
{
450
451 2
    return $mb ? mb_strlen($string) : strlen($string);
452
}
453
454
/**
455
 * Returns an array with the lines. Cross-platform compatible
456
 *
457
 * @param string $string
458
 *
459
 * @return array
460
 * @author Aurimas Niekis <[email protected]>
461
 */
462
function lines($string)
463
{
464 1
    return preg_split('/\r\n|\n|\r/', $string);
465
}
466
467
468
/**
469
 * Converts string first char to lowercase
470
 *
471
 * @param string $input
472
 *
473
 * @return string
474
 *
475
 * @author Aurimas Niekis <[email protected]>
476
 */
477
function lowerCaseFirst($input)
478
{
479 11
    return lcfirst($input);
480
}
481
482
483
/**
484
 * Pads the string in the center with specified character. char may be a string or a number, defaults is a space
485
 *
486
 * @param string $string
487
 * @param int    $length
488
 * @param string $char
489
 *
490
 * @return string
491
 * @author Aurimas Niekis <[email protected]>
492
 */
493
function pad($string, $length, $char = ' ')
494
{
495 6
    return str_pad($string, $length, $char, STR_PAD_BOTH);
496
}
497
498
499
/**
500
 * Left pads the string
501
 *
502
 * @param string $input
503
 * @param string $length
504
 * @param string $char
505
 *
506
 * @return string
507
 *
508
 * @author Lucantis Swann <[email protected]>
509
 */
510
function padLeft($input, $length, $char = ' ')
511
{
512 5
    return str_pad($input, $length, $char, STR_PAD_LEFT);
513
}
514
515
516
/**
517
 * Right pads the string
518
 *
519
 * @param string $input
520
 * @param string $length
521
 * @param string $char
522
 *
523
 * @return string
524
 *
525
 * @author Lucantis Swann <[email protected]>
526
 */
527
function padRight($input, $length, $char = ' ')
528
{
529 5
    return str_pad($input, $length, $char, STR_PAD_RIGHT);
530
}
531
532
533
/**
534
 * Repeat the string n times
535
 *
536
 * @param string $input
537
 * @param int    $n
538
 *
539
 * @return string
540
 *
541
 * @author Lucantis Swann <[email protected]>
542
 */
543
function repeat($input, $n)
544
{
545 6
    return str_repeat($input, $n);
546
}
547
548
/**
549
 * Reverses a string
550
 *
551
 * @param  string $input
552
 *
553
 * @return string
554
 * @author Rod Elias <[email protected]>
555
 */
556
function reverse($input)
557
{
558 2
    return strrev($input);
559
}
560
561
562
/**
563
 * Return the substring denoted by n positive right-most characters
564
 *
565
 * @param string $string
566
 * @param int    $n
567
 *
568
 * @return string
569
 * @author Aurimas Niekis <[email protected]>
570
 */
571 View Code Duplication
function right($string, $n)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
572
{
573 3
    $start = -$n;
574 3
    if ($n < 0) {
575 1
        $start = 0;
576 1
        $n     = -$n;
577 1
    }
578
579 3
    return substr($string, $start, $n);
580
}
581
582
583
/**
584
 * Converts the text into a valid url slug. Removes accents from Latin characters
585
 *
586
 * @param string $string
587
 *
588
 * @return string
589
 * @author Aurimas Niekis <[email protected]>
590
 */
591
function slugify($string)
592
{
593 2
    $string = latinize($string);
594 2
    $string = preg_replace('~[^\\pL\d]+~u', '-', $string);
595 2
    $string = trim($string, '-');
596 2
    $string = strtolower($string);
597
598 2
    return preg_replace('~[^-\w]+~', '', $string);
599
}
600
601
602
/**
603
 * Check if string starts with substring
604
 *
605
 * @param string $input
606
 * @param string $substring
607
 *
608
 * @return bool
609
 *
610
 * @author Lucantis Swann <[email protected]>
611
 */
612
function startsWith($input, $substring)
613
{
614 9
    return mb_strpos($input, $substring) === 0;
615
}
616
617
/**
618
 * Returns a new string with all occurrences of [string1],[string2],... removed.
619
 *
620
 * @param string $string
621
 * @param string $string1
622
 *
623
 * @return string
624
 * @author Aurimas Niekis <[email protected]>
625
 */
626
function strip($string, $string1)
0 ignored issues
show
Unused Code introduced by
The parameter $string1 is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
627
{
628 2
    $arguments = func_get_args();
629
630 2
    return str_replace(array_slice($arguments, 1), '', $string);
631
}
632
633
634
/**
635
 * Strip all of the punctuation
636
 *
637
 * @param string $string
638
 *
639
 * @return string
640
 * @author Aurimas Niekis <[email protected]>
641
 */
642
function stripPunctuation($string)
643
{
644 1
    return preg_replace('/[^\w\s]|_/', '', $string);
645
}
646
647
/**
648
 * Makes a case swapped version of the string
649
 * @param  string  $string the input string
650
 * @param  boolean $mb     to use or not to use multibyte character feature
651
 * @return string          case swapped version of the input string
652
 *
653
 * @author Rod Elias <[email protected]>
654
 */
655
function swapCase($string, $mb = false)
656
{
657
    return array_reduce(str_split($string), function($carry, $item) use ($mb) {
658 8
        return $carry .= isLower($item, $mb) ? toUpper($item, $mb) : toLower($item, $mb);
659 8
    }, '');
660
}
661
662
/**
663
 * Repeat the string n times
664
 *
665
 * @param string $input
666
 * @param int    $n
667
 *
668
 * @return string
669
 *
670
 * @author Lucantis Swann <[email protected]>
671
 */
672
function times($input, $n)
673
{
674 3
    return repeat($input, $n);
675
}
676
677
/**
678
 * Creates a title version of the string. Capitalizes all the words and replaces some characters in the string to
679
 * create a nicer looking title. string_titleize is meant for creating pretty output
680
 *
681
 * @param string $string
682
 * @param array  $ignore
683
 *
684
 * @return string
685
 * @author Aurimas Niekis <[email protected]>
686
 */
687
function titleize($string, array $ignore = [])
688
{
689 4
    $string = preg_replace('/(?<!^)([A-Z])/', ' $1', $string);
690 4
    $string = preg_replace('/[^a-z0-9:]+/i', ' ', $string);
691 4
    $string = trim($string);
692
693 4
    return preg_replace_callback(
694 4
        '/([\S]+)/u',
695
        function ($match) use ($ignore) {
696 4
            if (in_array(strtolower($match[0]), $ignore)) {
697 1
                return $match[0];
698
            } else {
699 4
                return ucfirst(strtolower($match[0]));
700
            }
701 4
        },
702
        $string
703 4
    );
704
}
705
706
/**
707
 * Makes a string lowercase
708
 * @param  string  $input the input string
709
 * @param  boolean $mb    to use or not to use multibyte character feature
710
 * @return string         lowercased string
711
 *
712
 * @author Rod Elias <[email protected]>
713
 */
714
function toLower($input, $mb = false)
715
{
716 13
    return $mb ? mb_strtolower($input, mb_detect_encoding($input, 'auto')) : strtolower($input);
717
}
718
719
/**
720
 * Join an array into a human readable sentence
721
 *
722
 * @param array  $array
723
 * @param string $delimiter
724
 * @param string $lastDelimiter
725
 *
726
 * @return string
727
 * @author Aurimas Niekis <[email protected]>
728
 */
729
function toSentence($array, $delimiter = ', ', $lastDelimiter = ' and ')
730
{
731 2
    $lastWord = array_pop($array);
732
733 2
    return implode($delimiter, $array) . $lastDelimiter . $lastWord;
734
}
735
736
/**
737
 * The same as string_to_sentence, but adjusts delimeters to use Serial comma)
738
 *
739
 * @param array  $array
740
 * @param string $delimiter
741
 * @param string $lastDelimiter
742
 *
743
 * @return string
744
 * @author Aurimas Niekis <[email protected]>
745
 */
746
function toSentenceSerial($array, $delimiter = ', ', $lastDelimiter = ' and ')
747
{
748 3
    $lastWord = array_pop($array);
749
750 3
    $lastDel = '';
751 3
    if (count($array) > 1) {
752 2
        $lastDel = trim($delimiter, ' ');
753 2
    }
754
755 3
    return implode($delimiter, $array) . $lastDel . $lastDelimiter . $lastWord;
756
}
757
758
759
/**
760
 * makes a string uppercase
761
 * @param  string  $input the input string
762
 * @param  boolean $mb    to use or not to use multibyte character feature
763
 * @return string         uppercased string
764
 *
765
 * @author Rod Elias <[email protected]>
766
 */
767
function toUpper($input, $mb = false)
768
{
769 13
    return $mb ? mb_strtoupper($input, mb_detect_encoding($input, 'auto')) : strtoupper($input);
770
}
771
772
773
/**
774
 * Truncate string accounting for word placement and character count
775
 *
776
 * @param  string $input
777
 * @param  int    $length
778
 * @param  string $chars
779
 *
780
 * @return string
781
 *
782
 * @author Lucantis Swann <[email protected]>
783
 */
784
function truncate($input, $length, $chars = '…')
785
{
786 3
    if (strlen($input) > $length) {
787 2
        $splits = preg_split('/([\s\n\r]+)/u', $input, null, PREG_SPLIT_DELIM_CAPTURE);
788
789 2
        $splitsLength = 0;
790 2
        $splitsCount  = count($splits);
791
792 2
        for ($lastSplit = 0; $lastSplit < $splitsCount; ++$lastSplit) {
793 2
            $splitsLength += strlen($splits[$lastSplit]);
794 2
            if ($splitsLength > $length) {
795 2
                break;
796
            }
797 2
        }
798
799 2
        return implode(array_slice($splits, 0, $lastSplit)) . $chars;
800
    } else {
801 1
        return $input;
802
    }
803
}
804
805
806
/**
807
 * Converts hyphens and camel casing to underscores
808
 *
809
 * @param string $string
810
 *
811
 * @return string
812
 * @author Aurimas Niekis <[email protected]>
813
 */
814
function underscore($string)
815
{
816 2
    return strtolower(preg_replace('/(?<!^)([A-Z])/', '_$1', str_replace('-', '_', $string)));
817
}
818
819
820
/**
821
 * Converts string first char to uppercase
822
 *
823
 * @param string $input
824
 *
825
 * @return string
826
 *
827
 * @author Aurimas Niekis <[email protected]>
828
 */
829
function upperCaseFirst($input)
830
{
831 19
    return ucfirst($input);
832
}
833