-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathStr.php
More file actions
513 lines (447 loc) · 10.9 KB
/
Copy pathStr.php
File metadata and controls
513 lines (447 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
<?php
declare(strict_types=1);
namespace Bow\Support;
use ForceUTF8\Encoding;
use Ramsey\Uuid\Uuid;
class Str
{
/**
* camel
*
* @param string $str
* @return string
*/
public static function camel(string $str): string
{
$parts = preg_split('/(_|-|\s)+/', $str);
$camel = "";
foreach ($parts as $key => $value) {
if ($key == 0) {
$camel .= $value;
continue;
}
$camel .= ucfirst($value);
}
return $camel;
}
/**
* Snake case
*
* @param string $str
* @param string $delimiter
* @return string
*/
public static function snake(string $str, string $delimiter = '_'): string
{
$str = preg_replace('/\s+/u', $delimiter, $str);
$str = static::lower(
preg_replace_callback(
'/([A-Z])/u',
function ($math) use ($delimiter) {
return $delimiter . static::lower($math[1]);
},
$str
)
);
return trim(preg_replace('/' . $delimiter . '{2,}/', $delimiter, $str), $delimiter);
}
/**
* lower case
*
* @param string $str
* @return string
*/
public static function lower(string $str): string
{
return mb_strtolower($str, 'UTF-8');
}
/**
* Get str plural
*
* @param string $str
* @return string
*/
public static function plural(string $str): string
{
if (str_ends_with($str, 'y')) {
$str = static::slice($str, 0, static::len($str) - 1);
return $str . 'ies';
}
str_ends_with($str, 's') ?: $str = $str . 's';
return $str;
}
/**
* slice
*
* @param string $str
* @param int $start
* @param int|null $length
* @return string
*/
public static function slice(string $str, int $start, ?int $length = null): string
{
$slice_str = '';
if ($length === null) {
$length = static::len($str);
}
if ($start < $length) {
$slice_str = mb_substr($str, $start, $length, 'UTF-8');
}
return $slice_str;
}
/**
* Len
*
* @param string $str
* @return int
*/
public static function len(string $str): int
{
return mb_strlen($str, 'UTF-8');
}
/**
* Contains
*
* @param string $search
* @param string $str
* @return bool
*/
public static function contains(string $search, string $str): bool
{
if ($search === $str) {
return true;
}
return (bool)static::pos($search, $str);
}
/**
* Get the string position
*
* @param string $search
* @param string $string
* @param int $offset
* @return int
*/
public static function pos(string $search, string $string, int $offset = 0): int
{
return mb_strpos($string, $search, $offset, 'UTF-8');
}
/**
* replace
*
* @param string $pattern
* @param string $replaceBy
* @param string $str
* @return string
*/
public static function replace(string $pattern, string $replaceBy, string $str): string
{
return str_replace($pattern, $replaceBy, $str);
}
/**
* capitalize
*
* @param string $str
* @return string
*/
public static function capitalize(string $str): string
{
return ucwords($str);
}
/**
* Wordily
*
* @param string $str
* @param string $sep
* @return array
*/
public static function wordily(string $str, string $sep = ' '): array
{
return static::split($sep, $str, static::count($sep, $str));
}
/**
* split
*
* @param string $pattern
* @param string $str
* @param int|null $limit
* @return array
*/
public static function split(string $pattern, string $str, ?int $limit = null): array
{
return mb_split($pattern, $str, $limit);
}
/**
* Returns the number of characters in a string.
*
* @param string $pattern
* @param string $str
* @return int
*/
public static function count(string $pattern, string $str): int
{
return count(explode($pattern, $str)) - 1;
}
/**
* Lists the string of characters in a specified number
*
* @param string $str
* @param int $number
* @return string
*/
public static function repeat(string $str, int $number): string
{
return str_repeat($str, $number);
}
/**
* Randomize
*
* @param int $size
* @return string
*/
public static function random(int $size = 16): string
{
return static::slice(str_shuffle('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'), 0, $size);
}
/**
* Get random uuid
*
* @return string
*/
public static function uuid(): string
{
return Uuid::uuid4()->toString();
}
/**
* Alias of slugify
*
* @param string $str
* @param string $delimiter
* @return string
*/
public static function slug(string $str, string $delimiter = '-'): string
{
return static::slugify($str, $delimiter);
}
/**
* slugify slug creator using a simple chain.
* eg: 'I am a string of character' => 'i-am-a-chain-of-character'
*
* @param string $str
* @param string $delimiter
* @return string
*/
public static function slugify(string $str, string $delimiter = '-'): string
{
$temp = preg_replace(
'/[^a-z0-9]/',
$delimiter,
strtolower(trim(strip_tags($str)))
);
return preg_replace('/-{2,}/', $delimiter, $temp);
}
/**
* Alias of un-slugify
*
* @param string $str
* @return string
*/
public static function unSlug(string $str): string
{
return static::unSlugify($str);
}
/**
* un-slugify, Lets you undo a slug
*
* @param string $str
* @return string
*/
public static function unSlugify(string $str): string
{
return preg_replace('/[^a-z0-9]/', ' ', strtolower(trim(strip_tags($str))));
}
/**
* Check if the email is a valid email.
*
* eg: example@email.com => true
*
* @param string $email
* @return bool
*/
public static function isMail(string $email): bool
{
$parts = explode('@', $email);
if (count($parts) != 2) {
return false;
}
return (bool)filter_var($email, FILTER_VALIDATE_EMAIL);
}
/**
* Check if the string is a domain
*
* eg: http://example.com => true
* eg: http:/example.com => false
*
* @param string $domain
* @return bool
*/
public static function isDomain(string $domain): bool
{
return (bool)preg_match(
'/^((https?|ftps?|ssl|url|git):\/\/)?[a-zA-Z0-9-_.]+\.[a-z]{2,6}$/',
$domain
);
}
/**
* Check if the string is in alphanumeric
*
* @param string $str
* @return bool
*/
public static function isAlphaNum(string $str): bool
{
return (bool)preg_match('/^[a-zA-Z0-9]+$/', $str);
}
/**
* Check if the string is in numeric
*
* @param string $str
* @return bool
*/
public static function isNumeric(string $str): bool
{
return (bool)preg_match('/^[0-9]+(\.[0-9]+)?$/', $str);
}
/**
* Check if the string is in alpha
*
* @param string $str
* @return bool
*/
public static function isAlpha(string $str): bool
{
return (bool)preg_match('/^[a-zA-Z]+$/', $str);
}
/**
* Check if the string is in slug format
*
* @param string $str
* @return bool
*/
public static function isSlug(string $str): bool
{
return (bool)preg_match('/^[a-z0-9-]+[a-z0-9]+$/', $str);
}
/**
* Check if the string is in uppercase
*
* @param string $str
* @return bool
*/
public static function isUpper(string $str): bool
{
return static::upper($str) === $str;
}
/**
* upper case
*
* @param string $str
* @return string
*/
public static function upper(string $str): string
{
return mb_strtoupper($str, 'UTF-8');
}
/**
* Check if the string is lowercase
*
* @param string $str
* @return bool
*/
public static function isLower(string $str): bool
{
return static::lower($str) === $str;
}
/**
* Returns a determined number of words in a string.
*
* @param string $words
* @param int $len
* @return string
*/
public static function words(string $words, int $len): string
{
$wordParts = explode(' ', $words);
$sentence = '';
for ($i = 0; $i < $len; $i++) {
if (!isset($wordParts[$i])) {
break;
}
$sentence .= ' ' . $wordParts[$i];
}
return trim($sentence);
}
/**
* Returns a string of words whose words are mixed.
*
* @param string $words
* @return string
*/
public static function shuffleWords(string $words): string
{
$wordParts = explode(' ', trim($words));
$wordPartsLen = count($wordParts);
$rand = [];
do {
$r = rand(0, $wordPartsLen - 1);
if (!in_array($r, $rand)) {
$rand[] = $r;
}
} while (count($rand) != $wordPartsLen);
$sentence = '';
foreach ($rand as $word) {
$sentence .= $wordParts[$word] . ' ';
}
return trim($sentence);
}
/**
* Enables to force the encoding in utf-8
*
* @return void
*/
public static function forceInUTF8(): void
{
mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');
}
/**
* Enables to force the encoding in utf-8
*
* @param string $garbled_utf8_string
* @return string
*/
public static function fixUTF8(string $garbled_utf8_string): string
{
return Encoding::fixUTF8($garbled_utf8_string);
}
/**
* Check if the string is empty
*
* @param string $str
* @return bool
*/
public static function isEmpty(string $str): bool
{
return trim($str) === '' || $str === '' || $str === null || strlen($str) === 0;
}
/**
* __call
*
* @param string $method
* @param array $arguments
* @return mixed
*/
public function __call(string $method, array $arguments = [])
{
return call_user_func_array([static::class, $method], $arguments);
}
}