1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Strip new line breaks from a string |
5
|
|
|
* @param $str |
6
|
|
|
* @return string|array |
7
|
|
|
*/ |
8
|
|
|
function strip_nl($str) |
9
|
|
|
{ |
10
|
|
|
return str_replace("\n", "", str_replace("\r", "", $str)); |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Generate random string (password) from a different charset based on $secLevel. |
15
|
|
|
* $secLevel=0 [a-z] charset. |
16
|
|
|
* $secLevel=1 [a-z0-9] charset. |
17
|
|
|
* $secLevel=2 [A-Za-z0-9] charset. |
18
|
|
|
* $secLevel=3 [A-Za-z0-9-_$!+&%?=*#@] charset. |
19
|
|
|
* @param int $length |
20
|
|
|
* @param int $secLevel |
21
|
|
|
* @return string |
22
|
|
|
*/ |
23
|
|
|
function generateRandomPassword(int $length = 10, int $secLevel = 2) : string |
24
|
|
|
{ |
25
|
|
|
$charset = 'abcdefghijklmnopqrstuvwxyz'; |
26
|
|
|
if ($secLevel == 1) { |
27
|
|
|
$charset = 'abcdefghijklmnopqrstuvwxyz1234567890'; |
28
|
|
|
} elseif ($secLevel == 2) { |
29
|
|
|
$charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890'; |
30
|
|
|
} elseif ($secLevel >= 3) { |
31
|
|
|
$charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-_$!+&%?=*#@'; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return generateRandomString($length, '', $charset); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Generate random string from [0-9A-Za-z] charset. |
39
|
|
|
* You may extend charset by passing $extChars (i.e. add these chars to existing). |
40
|
|
|
* Ex.: $extChars='-_$!' implies that the final charset is [0-9A-Za-z-_$!] |
41
|
|
|
* If $newChars is set, the default charset are replaced by this and $extChars was ignored. |
42
|
|
|
* Ex.: $newChars='0123456789' implies that the final charset is [0123456789] |
43
|
|
|
* @param int $length |
44
|
|
|
* @param string $extChars |
45
|
|
|
* @param string $newChars |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
function generateRandomString(int $length = 10, string $extChars = '', string $newChars = '') : string |
49
|
|
|
{ |
50
|
|
|
if ($length < 1) { |
51
|
|
|
$length = 1; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
55
|
|
|
if ($newChars !== null && $newChars != '') { |
56
|
|
|
$characters = $newChars; |
57
|
|
|
} elseif ($extChars !== null && $extChars != '') { |
58
|
|
|
$characters .= $extChars; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$charactersLength = strlen($characters); |
62
|
|
|
$randomString = ''; |
63
|
|
|
|
64
|
|
|
for ($i = 0; $i < $length; $i++) { |
65
|
|
|
$randomString .= $characters[random_int(0, $charactersLength - 1)]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $randomString; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Javascript escape |
73
|
|
|
* @param string $str |
74
|
|
|
* @return string |
75
|
|
|
* @source https://github.com/rtconner/laravel-plusplus/blob/laravel-5/src/plus-functions.php |
76
|
|
|
*/ |
77
|
|
|
function jse(string $str) : string |
78
|
|
|
{ |
79
|
|
|
if ($str === null || $str == '') { |
80
|
|
|
return ''; |
81
|
|
|
} |
82
|
|
|
$str = str_replace("\n", "", str_replace("\r", "", $str)); |
83
|
|
|
return addslashes($str); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (!function_exists('gravatar')) { |
87
|
|
|
/** |
88
|
|
|
* Get a Gravatar URL from email. |
89
|
|
|
* |
90
|
|
|
* @param string $email The email address |
91
|
|
|
* @param int $size in pixels, defaults to 80px [ 1 - 2048 ] |
92
|
|
|
* @param string $default imageset to use [ 404 | mm | identicon | monsterid | wavatar ] |
93
|
|
|
* @param string $rating (inclusive) [ g | pg | r | x ] |
94
|
|
|
* @return string |
95
|
|
|
* @source http://gravatar.com/site/implement/images/php/ |
96
|
|
|
*/ |
97
|
|
|
function gravatar($email, $size = 80, $default = 'mm', $rating = 'g') |
98
|
|
|
{ |
99
|
|
|
$url = 'http://www.gravatar.com/avatar/'; |
100
|
|
|
$url .= md5(strtolower(trim($email))); |
101
|
|
|
$url .= "?s=$size&d=$default&r=$rating"; |
102
|
|
|
return $url; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* ***************************************************** |
108
|
|
|
* LARAVEL STRING HELPERS |
109
|
|
|
* With some adjustments |
110
|
|
|
* ***************************************************** |
111
|
|
|
*/ |
112
|
|
|
|
113
|
|
|
if (!function_exists('e')) { |
114
|
|
|
/** |
115
|
|
|
* Escape HTML entities in a string. |
116
|
|
|
* |
117
|
|
|
* @param string $value |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
function e($value) |
121
|
|
|
{ |
122
|
|
|
return htmlentities($value, ENT_QUOTES, 'UTF-8', false); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if (!function_exists('preg_replace_sub')) { |
127
|
|
|
/** |
128
|
|
|
* Replace a given pattern with each value in the array in sequentially. |
129
|
|
|
* |
130
|
|
|
* @param string $pattern |
131
|
|
|
* @param array $replacements |
132
|
|
|
* @param string $subject |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
function preg_replace_sub($pattern, &$replacements, $subject) |
136
|
|
|
{ |
137
|
|
|
return preg_replace_callback($pattern, function () use (&$replacements) { |
138
|
|
|
return array_shift($replacements); |
139
|
|
|
}, $subject); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if (!function_exists('snake_case')) { |
144
|
|
|
/** |
145
|
|
|
* Convert a string to snake case. |
146
|
|
|
* |
147
|
|
|
* @param string $value |
148
|
|
|
* @param string $delimiter |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
|
|
function snake_case($value, $delimiter = '_') |
152
|
|
|
{ |
153
|
|
|
$snakeCache = []; |
154
|
|
|
$key = $value . $delimiter; |
155
|
|
|
if (isset($snakeCache[$key])) { |
156
|
|
|
return $snakeCache[$key]; |
157
|
|
|
} |
158
|
|
|
if (!ctype_lower($value)) { |
159
|
|
|
$value = strtolower(preg_replace('/(.)(?=[A-Z])/', '$1' . $delimiter, $value)); |
160
|
|
|
} |
161
|
|
|
return $snakeCache[$key] = $value; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
if (!function_exists('str_random')) { |
166
|
|
|
/** |
167
|
|
|
* Generate a more truly "random" alpha-numeric string with openssl. |
168
|
|
|
* If openssl_random_pseudo_bytes not exists, use simple legacy function |
169
|
|
|
* |
170
|
|
|
* @param int $length |
171
|
|
|
* @return string |
172
|
|
|
* |
173
|
|
|
* @throws \RuntimeException |
174
|
|
|
*/ |
175
|
|
|
function str_random(int $length = 16) |
176
|
|
|
{ |
177
|
|
|
if ($length < 0) { |
178
|
|
|
$length = 16; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
if (!function_exists('openssl_random_pseudo_bytes')) { |
182
|
|
|
return generateRandomString($length); |
183
|
|
|
} |
184
|
|
|
$bytes = openssl_random_pseudo_bytes($length * 2); |
185
|
|
|
if ($bytes === false) { |
186
|
|
|
throw new RuntimeException('Unable to generate random string.'); |
187
|
|
|
} |
188
|
|
|
return substr(str_replace(array('/', '+', '='), '', base64_encode($bytes)), 0, $length); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
if (!function_exists('ends_with')) { |
193
|
|
|
/** |
194
|
|
|
* Determine if a given string ends with a given substring. |
195
|
|
|
* |
196
|
|
|
* @param string $haystack |
197
|
|
|
* @param string|array $needles |
198
|
|
|
* @return bool |
199
|
|
|
*/ |
200
|
|
|
function ends_with($haystack, $needles) |
201
|
|
|
{ |
202
|
|
View Code Duplication |
if ($haystack === null || $haystack == '' || $needles === null || $needles == '') { |
|
|
|
|
203
|
|
|
return false; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
foreach ((array)$needles as $needle) { |
207
|
|
|
if ((string)$needle === substr($haystack, -strlen($needle))) { |
208
|
|
|
return true; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
return false; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
if (!function_exists('starts_with')) { |
216
|
|
|
/** |
217
|
|
|
* Determine if a given string starts with a given substring. |
218
|
|
|
* |
219
|
|
|
* @param string $haystack |
220
|
|
|
* @param string|array $needles |
221
|
|
|
* @return bool |
222
|
|
|
*/ |
223
|
|
|
function starts_with($haystack, $needles) |
224
|
|
|
{ |
225
|
|
View Code Duplication |
if ($haystack === null || $haystack == '' || $needles === null || $needles == '') { |
|
|
|
|
226
|
|
|
return false; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
View Code Duplication |
foreach ((array)$needles as $needle) { |
|
|
|
|
230
|
|
|
if ($needle != '' && strpos($haystack, $needle) === 0) { |
231
|
|
|
return true; |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
return false; |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
if (!function_exists('str_contains')) { |
239
|
|
|
/** |
240
|
|
|
* Determine if a given string contains a given substring. |
241
|
|
|
* |
242
|
|
|
* @param string $haystack |
243
|
|
|
* @param string|array $needles |
244
|
|
|
* @return bool |
245
|
|
|
*/ |
246
|
|
|
function str_contains($haystack, $needles) |
247
|
|
|
{ |
248
|
|
View Code Duplication |
foreach ((array)$needles as $needle) { |
|
|
|
|
249
|
|
|
if ($needle != '' && strpos($haystack, $needle) !== false) { |
250
|
|
|
return true; |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
return false; |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
if (!function_exists('str_finish')) { |
258
|
|
|
/** |
259
|
|
|
* Cap a string with a single instance of a given value. |
260
|
|
|
* |
261
|
|
|
* @param string $value |
262
|
|
|
* @param string $cap |
263
|
|
|
* @return string |
264
|
|
|
*/ |
265
|
|
|
function str_finish($value, $cap) |
266
|
|
|
{ |
267
|
|
|
$quoted = preg_quote($cap, '/'); |
268
|
|
|
return preg_replace('/(?:' . $quoted . ')+$/', '', $value) . $cap; |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
if (!function_exists('str_is')) { |
273
|
|
|
/** |
274
|
|
|
* Determine if a given string matches a given pattern. |
275
|
|
|
* |
276
|
|
|
* @param string $pattern |
277
|
|
|
* @param string $value |
278
|
|
|
* @return bool |
279
|
|
|
*/ |
280
|
|
|
function str_is($pattern, $value) |
281
|
|
|
{ |
282
|
|
|
if ($pattern == $value) { |
283
|
|
|
return true; |
284
|
|
|
} |
285
|
|
|
$pattern = preg_quote($pattern, '#'); |
286
|
|
|
// Asterisks are translated into zero-or-more regular expression wildcards |
287
|
|
|
// to make it convenient to check if the strings starts with the given |
288
|
|
|
// pattern such as "library/*", making any string check convenient. |
289
|
|
|
$pattern = str_replace('\*', '.*', $pattern) . '\z'; |
290
|
|
|
return preg_match('#^' . $pattern . '#', $value) === 1; |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
if (!function_exists('str_limit')) { |
294
|
|
|
/** |
295
|
|
|
* Limit the number of characters in a string. |
296
|
|
|
* |
297
|
|
|
* @param string $value |
298
|
|
|
* @param int $limit |
299
|
|
|
* @param string $end |
300
|
|
|
* @return string |
301
|
|
|
*/ |
302
|
|
|
function str_limit($value, $limit = 100, $end = '...') |
303
|
|
|
{ |
304
|
|
|
if (mb_strlen($value) <= $limit) { |
305
|
|
|
return $value; |
306
|
|
|
} |
307
|
|
|
return rtrim(mb_substr($value, 0, $limit, 'UTF-8')) . $end; |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
if (!function_exists('str_replace_array')) { |
312
|
|
|
/** |
313
|
|
|
* Replace a given value in the string sequentially with an array. |
314
|
|
|
* |
315
|
|
|
* @param string $search |
316
|
|
|
* @param array $replace |
317
|
|
|
* @param string $subject |
318
|
|
|
* @return string |
319
|
|
|
*/ |
320
|
|
|
function str_replace_array($search, array $replace, $subject) |
321
|
|
|
{ |
322
|
|
|
foreach ($replace as $value) { |
323
|
|
|
$subject = preg_replace('/' . $search . '/', $value, $subject, 1); |
324
|
|
|
} |
325
|
|
|
return $subject; |
326
|
|
|
} |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
if (!function_exists('studly_case')) { |
330
|
|
|
/** |
331
|
|
|
* Convert a value to studly caps case. |
332
|
|
|
* |
333
|
|
|
* @param string $value |
334
|
|
|
* @return string |
335
|
|
|
*/ |
336
|
|
|
function studly_case($value) |
337
|
|
|
{ |
338
|
|
|
$studlyCache = []; |
339
|
|
|
$key = $value; |
340
|
|
|
if (isset($studlyCache[$key])) { |
341
|
|
|
return $studlyCache[$key]; |
342
|
|
|
} |
343
|
|
|
$value = ucwords(str_replace(array('-', '_'), ' ', $value)); |
344
|
|
|
return $studlyCache[$key] = str_replace(' ', '', $value); |
345
|
|
|
} |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
if (!function_exists('studly')) { |
349
|
|
|
/** |
350
|
|
|
* Convert a value to studly caps case. |
351
|
|
|
* Alias of studly_case |
352
|
|
|
* |
353
|
|
|
* @param string $value |
354
|
|
|
* @return string |
355
|
|
|
*/ |
356
|
|
|
function studly($value) |
357
|
|
|
{ |
358
|
|
|
return studly_case($value); |
359
|
|
|
} |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
if (!function_exists('camel_case')) { |
363
|
|
|
/** |
364
|
|
|
* Convert a value to camel case. |
365
|
|
|
* |
366
|
|
|
* @param string $value |
367
|
|
|
* @return string |
368
|
|
|
*/ |
369
|
|
|
function camel_case($value) |
370
|
|
|
{ |
371
|
|
|
$camelCache = []; |
372
|
|
|
if (isset($camelCache[$value])) { |
373
|
|
|
return $camelCache[$value]; |
374
|
|
|
} |
375
|
|
|
return $camelCache[$value] = lcfirst(studly($value)); |
376
|
|
|
} |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* Replace underscores with dashes in the string. |
381
|
|
|
* @param string $word |
382
|
|
|
* @return string |
383
|
|
|
*/ |
384
|
|
|
function underscore2dash(string $word) : string |
385
|
|
|
{ |
386
|
|
|
return str_replace('_', '-', $word); |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* Make an underscored, lowercase form from the expression in the string. |
391
|
|
|
* @param string $word |
392
|
|
|
* @return string |
393
|
|
|
*/ |
394
|
|
|
function dash2underscore(string $word) : string |
395
|
|
|
{ |
396
|
|
|
$word = preg_replace('/([A-Z]+)([A-Z][a-z])/', '\1_\2', $word); |
397
|
|
|
$word = preg_replace('/([a-z])([A-Z])/', '\1_\2', $word); |
398
|
|
|
return str_replace('-', '_', strtolower($word)); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* Replace multiple spaces with one space. |
403
|
|
|
* @param string $str |
404
|
|
|
* @return string |
405
|
|
|
*/ |
406
|
|
|
function str_replace_multiple_space(string $str) : string |
407
|
|
|
{ |
408
|
|
|
return preg_replace('/\s+/', ' ', $str); |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* Replace last occurrence ($search) of a string ($subject) with $replace string. |
413
|
|
|
* @param string $search |
414
|
|
|
* @param string $replace |
415
|
|
|
* @param string $subject |
416
|
|
|
* @return string |
417
|
|
|
*/ |
418
|
|
|
function str_replace_last(string $search, string $replace, string $subject) : string |
419
|
|
|
{ |
420
|
|
|
if ($search == '') { |
421
|
|
|
return $subject; |
422
|
|
|
} |
423
|
|
|
$position = strrpos($subject, $search); |
424
|
|
|
if ($position === false) { |
425
|
|
|
return $subject; |
426
|
|
|
} |
427
|
|
|
return substr_replace($subject, $replace, $position, strlen($search)); |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* Get a segment from a string based on a delimiter. |
432
|
|
|
* Returns an empty string when the offset doesn't exist. |
433
|
|
|
* Use a negative index to start counting from the last element. |
434
|
|
|
* |
435
|
|
|
* @param string $delimiter |
436
|
|
|
* @param int $index |
437
|
|
|
* @param string $subject |
438
|
|
|
* |
439
|
|
|
* @return string |
440
|
|
|
* @see https://github.com/spatie/string/blob/master/src/Str.php |
441
|
|
|
*/ |
442
|
|
|
function segment($delimiter, $index, $subject) |
443
|
|
|
{ |
444
|
|
|
$segments = explode($delimiter, $subject); |
445
|
|
|
if ($index < 0) { |
446
|
|
|
$segments = array_reverse($segments); |
447
|
|
|
$index = (int)abs($index) - 1; |
448
|
|
|
} |
449
|
|
|
$segment = isset($segments[$index]) ? $segments[$index] : ''; |
450
|
|
|
return $segment; |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
/** |
454
|
|
|
* Get the first segment from a string based on a delimiter. |
455
|
|
|
* |
456
|
|
|
* @param string $delimiter |
457
|
|
|
* @param string $subject |
458
|
|
|
* |
459
|
|
|
* @return string |
460
|
|
|
* @see https://github.com/spatie/string/blob/master/src/Str.php |
461
|
|
|
*/ |
462
|
|
|
function firstSegment($delimiter, $subject) : string |
463
|
|
|
{ |
464
|
|
|
return segment($delimiter, 0, $subject); |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
/** |
468
|
|
|
* Get the last segment from a string based on a delimiter. |
469
|
|
|
* |
470
|
|
|
* @param string $delimiter |
471
|
|
|
* @param string $subject |
472
|
|
|
* |
473
|
|
|
* @return string |
474
|
|
|
* @see https://github.com/spatie/string/blob/master/src/Str.php |
475
|
|
|
*/ |
476
|
|
|
function lastSegment($delimiter, $subject) : string |
477
|
|
|
{ |
478
|
|
|
return segment($delimiter, -1, $subject); |
479
|
|
|
} |
480
|
|
|
|
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.