1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Flextype\Component\Strings; |
6
|
|
|
|
7
|
|
|
use function ctype_lower; |
8
|
|
|
use function implode; |
9
|
|
|
use function lcfirst; |
10
|
|
|
use function mb_convert_case; |
11
|
|
|
use function mb_strimwidth; |
12
|
|
|
use function mb_strlen; |
13
|
|
|
use function mb_strpos; |
14
|
|
|
use function mb_strtolower; |
15
|
|
|
use function mb_strtoupper; |
16
|
|
|
use function mb_strwidth; |
17
|
|
|
use function mb_substr; |
18
|
|
|
use function preg_match; |
19
|
|
|
use function preg_replace; |
20
|
|
|
use function random_int; |
21
|
|
|
use function rtrim; |
22
|
|
|
use function str_replace; |
23
|
|
|
use function trim; |
24
|
|
|
use function ucwords; |
25
|
|
|
|
26
|
|
|
use const MB_CASE_TITLE; |
27
|
|
|
|
28
|
|
|
class Strings |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* The cache for words. |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected static $cache = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Removes any leading and traling slashes from a string. |
39
|
|
|
* |
40
|
|
|
* @param string $string String with slashes |
41
|
|
|
*/ |
42
|
|
|
public static function trimSlashes(string $string): string |
43
|
|
|
{ |
44
|
|
|
return trim($string, '/'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Reduces multiple slashes in a string to single slashes. |
49
|
|
|
* |
50
|
|
|
* @param string $string String or array of strings with slashes |
51
|
|
|
*/ |
52
|
|
|
public static function reduceSlashes(string $string): string |
53
|
|
|
{ |
54
|
|
|
return preg_replace('#(?<!:)//+#', '/', $string); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Removes single and double quotes from a string. |
59
|
|
|
* |
60
|
|
|
* @param string $str String with single and double quotes |
61
|
|
|
*/ |
62
|
|
|
public static function stripQuotes(string $string): string |
63
|
|
|
{ |
64
|
|
|
return str_replace(['"', "'"], '', $string); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Convert single and double quotes to entities. |
69
|
|
|
* |
70
|
|
|
* @param string $string String with single and double quotes |
71
|
|
|
*/ |
72
|
|
|
public static function quotesToEntities(string $string): string |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
return str_replace(["\'", '"', "'", '"'], [''', '"', ''', '"'], $str); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Creates a random string of characters. |
79
|
|
|
* |
80
|
|
|
* @param int $length The number of characters. Default is 16 |
81
|
|
|
* @param string $keyspace The keyspace |
82
|
|
|
*/ |
83
|
|
|
public static function random(int $length = 64, string $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'): string |
84
|
|
|
{ |
85
|
|
|
if ($length <= 0) { |
86
|
|
|
$length = 1; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$pieces = []; |
90
|
|
|
$max = mb_strlen($keyspace, '8bit') - 1; |
91
|
|
|
|
92
|
|
|
for ($i = 0; $i < $length; ++$i) { |
93
|
|
|
$pieces[] = $keyspace[random_int(0, $max)]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return implode('', $pieces); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Add's _1 to a string or increment the ending number to allow _2, _3, etc. |
101
|
|
|
* |
102
|
|
|
* @param string $string String to increment |
103
|
|
|
* @param int $first Start with |
104
|
|
|
* @param string $separator Separator |
105
|
|
|
*/ |
106
|
|
|
public static function increment(string $string, int $first = 1, string $separator = '_'): string |
107
|
|
|
{ |
108
|
|
|
preg_match('/(.+)' . $separator . '([0-9]+)$/', $string, $match); |
109
|
|
|
|
110
|
|
|
return isset($match[2]) ? $match[1] . $separator . ($match[2] + 1) : $string . $separator . $first; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Return the length of the given string. |
115
|
|
|
* |
116
|
|
|
* @param string $string String to check |
117
|
|
|
* @param string|null $encoding String encoding |
118
|
|
|
*/ |
119
|
|
|
public static function length(string $string, ?string $encoding = null): int |
120
|
|
|
{ |
121
|
|
|
if ($encoding) { |
122
|
|
|
return mb_strlen($string, $encoding); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return mb_strlen($string); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Limit the number of characters in a string. |
130
|
|
|
* |
131
|
|
|
* @param string $string String |
132
|
|
|
* @param int $limit Limit of characters |
133
|
|
|
* @param string $append Text to append to the string IF it gets truncated |
134
|
|
|
*/ |
135
|
|
|
public static function limit(string $string, int $limit = 100, string $append = '...'): string |
136
|
|
|
{ |
137
|
|
|
if (mb_strwidth($string, 'UTF-8') <= $limit) { |
138
|
|
|
return $string; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return rtrim(mb_strimwidth($string, 0, $limit, '', 'UTF-8')) . $append; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Convert the given string to lower-case. |
146
|
|
|
* |
147
|
|
|
* @param string $string String |
148
|
|
|
*/ |
149
|
|
|
public static function lower(string $string): string |
150
|
|
|
{ |
151
|
|
|
return mb_strtolower($string, 'UTF-8'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Convert the given string to upper-case. |
156
|
|
|
* |
157
|
|
|
* @param string $value |
158
|
|
|
*/ |
159
|
|
|
public static function upper(string $string): string |
160
|
|
|
{ |
161
|
|
|
return mb_strtoupper($string, 'UTF-8'); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Returns the portion of string specified by the start and length parameters. |
166
|
|
|
* |
167
|
|
|
* @param string $string The string to extract the substring from. |
168
|
|
|
* @param int $start If start is non-negative, the returned string will |
169
|
|
|
* start at the start'th position in $string, counting from zero. |
170
|
|
|
* For instance, in the string 'abcdef', the character at position |
171
|
|
|
* 0 is 'a', the character at position 2 is 'c', and so forth. |
172
|
|
|
* @param int|null $length Maximum number of characters to use from string. |
173
|
|
|
* If omitted or NULL is passed, extract all characters to the end of the string. |
174
|
|
|
*/ |
175
|
|
|
public static function substr(string $string, int $start, ?int $length = null): string |
176
|
|
|
{ |
177
|
|
|
return mb_substr($string, $start, $length, 'UTF-8'); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Convert a string to studly caps case. |
182
|
|
|
* |
183
|
|
|
* @param string $string String |
184
|
|
|
*/ |
185
|
|
|
public static function studly(string $string): string |
186
|
|
|
{ |
187
|
|
|
$key = $string; |
188
|
|
|
|
189
|
|
|
if (isset(static::$cache['studly'][$key])) { |
190
|
|
|
return static::$cache['studly'][$key]; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$string = ucwords(str_replace(['-', '_'], ' ', $string)); |
194
|
|
|
|
195
|
|
|
return static::$cache['studly'][$key] = str_replace(' ', '', $string); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Convert a string to snake case. |
200
|
|
|
* |
201
|
|
|
* @param string $string String |
202
|
|
|
* @param string $delimiter Delimeter |
203
|
|
|
*/ |
204
|
|
|
public static function snake(string $string, string $delimiter = '_'): string |
205
|
|
|
{ |
206
|
|
|
$key = $string; |
207
|
|
|
|
208
|
|
|
if (isset(static::$cache['snake'][$key][$delimiter])) { |
209
|
|
|
return static::$cache['snake'][$key][$delimiter]; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
if (! ctype_lower($string)) { |
213
|
|
|
$value = preg_replace('/\s+/u', '', ucwords($string)); |
|
|
|
|
214
|
|
|
|
215
|
|
|
$string = static::lower(preg_replace('/(.)(?=[A-Z])/u', '$1' . $delimiter, $string)); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return static::$cache['snake'][$key][$delimiter] = $string; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Convert a string to camel case. |
223
|
|
|
* |
224
|
|
|
* @param string $string String |
225
|
|
|
*/ |
226
|
|
|
public static function camel(string $string): string |
227
|
|
|
{ |
228
|
|
|
if (isset(static::$cache['camel'][$string])) { |
229
|
|
|
return static::$cache['camel'][$string]; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
return static::$cache['camel'][$string] = lcfirst(static::studly($string)); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Convert a string to kebab case. |
237
|
|
|
* |
238
|
|
|
* @param string $string String |
239
|
|
|
*/ |
240
|
|
|
public static function kebab(string $string): string |
241
|
|
|
{ |
242
|
|
|
return static::snake($string, '-'); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Limit the number of words in a string. |
247
|
|
|
* |
248
|
|
|
* @param string $string String |
249
|
|
|
* @param int $words Words limit |
250
|
|
|
* @param string $append Text to append to the string IF it gets truncated |
251
|
|
|
*/ |
252
|
|
|
public static function words(string $string, int $words = 100, string $append = '...'): string |
253
|
|
|
{ |
254
|
|
|
preg_match('/^\s*+(?:\S++\s*+){1,' . $words . '}/u', $string, $matches); |
255
|
|
|
|
256
|
|
|
if (! isset($matches[0]) || static::length($string) === static::length($matches[0])) { |
257
|
|
|
return $string; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
return rtrim($matches[0]) . $append; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Determine if a given string contains a given substring. |
265
|
|
|
* |
266
|
|
|
* @param string $haystack The string being checked. |
267
|
|
|
* @param string|string[] $needles The string to find in haystack. |
268
|
|
|
*/ |
269
|
|
|
public static function contains(string $haystack, $needles): bool |
270
|
|
|
{ |
271
|
|
|
foreach ((array) $needles as $needle) { |
272
|
|
|
if ($needle !== '' && mb_strpos($haystack, $needle) !== false) { |
273
|
|
|
return true; |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
return false; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Determine if a given string contains all array values. |
282
|
|
|
* |
283
|
|
|
* @param string $haystack The string being checked. |
284
|
|
|
* @param string[] $needles The array of strings to find in haystack. |
285
|
|
|
*/ |
286
|
|
|
public static function containsAll(string $haystack, array $needles): bool |
287
|
|
|
{ |
288
|
|
|
foreach ($needles as $needle) { |
289
|
|
|
if (! static::contains($haystack, $needle)) { |
290
|
|
|
return false; |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
return true; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Converts the first character of a string to upper case |
299
|
|
|
* and leaves the other characters unchanged. |
300
|
|
|
* |
301
|
|
|
* @param string $string String |
302
|
|
|
*/ |
303
|
|
|
public static function ucfirst(string $string): string |
304
|
|
|
{ |
305
|
|
|
return static::upper(static::substr($string, 0, 1)) . static::substr($string, 1); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Converts the first character of every word of string to upper case and the others to lower case. |
310
|
|
|
* |
311
|
|
|
* @param string $string String |
312
|
|
|
*/ |
313
|
|
|
public static function capitalize(string $string): string |
|
|
|
|
314
|
|
|
{ |
315
|
|
|
return mb_convert_case($s, MB_CASE_TITLE, 'UTF-8'); |
|
|
|
|
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Strip whitespace (or other characters) from the beginning and end of a string. |
320
|
|
|
* |
321
|
|
|
* @param string $string The string that will be trimmed. |
322
|
|
|
* @param string $character_mask Optionally, the stripped characters can also be |
323
|
|
|
* specified using the character_mask parameter.. |
324
|
|
|
*/ |
325
|
|
|
public static function trim(string $string, string $character_mask = " \t\n\r\0\x0B"): string |
326
|
|
|
{ |
327
|
|
|
return trim($string, $character_mask); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Reverses string. |
332
|
|
|
* |
333
|
|
|
* @param string $string String |
334
|
|
|
*/ |
335
|
|
|
public static function reverse(string $string): string |
336
|
|
|
{ |
337
|
|
|
$result = ''; |
338
|
|
|
|
339
|
|
|
for ($i = static::length($string); $i >= 0; $i--) { |
340
|
|
|
$result .= static::substr($string, $i, 1); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
return $result; |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.