PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.22.3
GiveWP – Donation Plugin and Fundraising Platform v2.22.3
4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / src / Framework / Support / Facades / Str.php
give / src / Framework / Support / Facades Last commit date
DateTime 4 years ago CurrencyFacade.php 4 years ago Facade.php 4 years ago Str.php 4 years ago
Str.php
819 lines
1 <?php
2
3 namespace Give\Framework\Support\Facades;
4
5 use function sanitize_title;
6
7 class Str
8 {
9 /**
10 * The cache of snake-cased words.
11 *
12 * @var array
13 */
14 protected static $snakeCache = [];
15
16 /**
17 * The cache of camel-cased words.
18 *
19 * @var array
20 */
21 protected static $camelCache = [];
22
23 /**
24 * The cache of studly-cased words.
25 *
26 * @var array
27 */
28 protected static $studlyCache = [];
29
30 /**
31 * Return the remainder of a string after the first occurrence of a given value.
32 *
33 * @param string $subject
34 * @param string $search
35 * @return string
36 */
37 public static function after($subject, $search)
38 {
39 return $search === '' ? $subject : array_reverse(explode($search, $subject, 2))[0];
40 }
41
42 /**
43 * Return the remainder of a string after the last occurrence of a given value.
44 *
45 * @param string $subject
46 * @param string $search
47 * @return string
48 */
49 public static function afterLast($subject, $search)
50 {
51 if ($search === '') {
52 return $subject;
53 }
54
55 $position = strrpos($subject, (string) $search);
56
57 if ($position === false) {
58 return $subject;
59 }
60
61 return substr($subject, $position + strlen($search));
62 }
63
64 /**
65 * Get the portion of a string before the first occurrence of a given value.
66 *
67 * @param string $subject
68 * @param string $search
69 * @return string
70 */
71 public static function before($subject, $search)
72 {
73 if ($search === '') {
74 return $subject;
75 }
76
77 $result = strstr($subject, (string) $search, true);
78
79 return $result === false ? $subject : $result;
80 }
81
82 /**
83 * Get the portion of a string before the last occurrence of a given value.
84 *
85 * @param string $subject
86 * @param string $search
87 * @return string
88 */
89 public static function beforeLast($subject, $search)
90 {
91 if ($search === '') {
92 return $subject;
93 }
94
95 $pos = mb_strrpos($subject, $search);
96
97 if ($pos === false) {
98 return $subject;
99 }
100
101 return static::substr($subject, 0, $pos);
102 }
103
104 /**
105 * Get the portion of a string between two given values.
106 *
107 * @param string $subject
108 * @param string $from
109 * @param string $to
110 * @return string
111 */
112 public static function between($subject, $from, $to)
113 {
114 if ($from === '' || $to === '') {
115 return $subject;
116 }
117
118 return static::beforeLast(static::after($subject, $from), $to);
119 }
120
121 /**
122 * Get the smallest possible portion of a string between two given values.
123 *
124 * @param string $subject
125 * @param string $from
126 * @param string $to
127 * @return string
128 */
129 public static function betweenFirst($subject, $from, $to)
130 {
131 if ($from === '' || $to === '') {
132 return $subject;
133 }
134
135 return static::before(static::after($subject, $from), $to);
136 }
137
138 /**
139 * Convert a value to camel case.
140 *
141 * @param string $value
142 * @return string
143 */
144 public static function camel($value)
145 {
146 if (isset(static::$camelCache[$value])) {
147 return static::$camelCache[$value];
148 }
149
150 return static::$camelCache[$value] = lcfirst(static::studly(static::lower($value)));
151 }
152
153 /**
154 * Determine if a given string contains a given substring.
155 *
156 * @param string $haystack
157 * @param string|string[] $needles
158 * @param bool $ignoreCase
159 * @return bool
160 */
161 public static function contains($haystack, $needles, $ignoreCase = false)
162 {
163 if ($ignoreCase) {
164 $haystack = mb_strtolower($haystack);
165 $needles = array_map('mb_strtolower', (array) $needles);
166 }
167
168 foreach ((array) $needles as $needle) {
169 if ($needle !== '' && str_contains($haystack, $needle)) {
170 return true;
171 }
172 }
173
174 return false;
175 }
176
177 /**
178 * Determine if a given string contains all array values.
179 *
180 * @param string $haystack
181 * @param string[] $needles
182 * @param bool $ignoreCase
183 * @return bool
184 */
185 public static function containsAll($haystack, array $needles, $ignoreCase = false)
186 {
187 if ($ignoreCase) {
188 $haystack = mb_strtolower($haystack);
189 $needles = array_map('mb_strtolower', $needles);
190 }
191
192 foreach ($needles as $needle) {
193 if (! static::contains($haystack, $needle)) {
194 return false;
195 }
196 }
197
198 return true;
199 }
200
201 /**
202 * Determine if a given string ends with a given substring.
203 *
204 * @param string $haystack
205 * @param string|string[] $needles
206 * @return bool
207 */
208 public static function endsWith($haystack, $needles)
209 {
210 foreach ((array) $needles as $needle) {
211 if (
212 $needle !== '' && $needle !== null
213 && str_ends_with($haystack, $needle)
214 ) {
215 return true;
216 }
217 }
218
219 return false;
220 }
221
222 /**
223 * Cap a string with a single instance of a given value.
224 *
225 * @param string $value
226 * @param string $cap
227 * @return string
228 */
229 public static function finish($value, $cap)
230 {
231 $quoted = preg_quote($cap, '/');
232
233 return preg_replace('/(?:'.$quoted.')+$/u', '', $value).$cap;
234 }
235
236 /**
237 * Determine if a given string is a valid UUID.
238 *
239 * @param string $value
240 * @return bool
241 */
242 public static function isUuid($value)
243 {
244 if (! is_string($value)) {
245 return false;
246 }
247
248 return preg_match('/^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/iD', $value) > 0;
249 }
250
251 /**
252 * Convert a string to kebab case.
253 *
254 * @param string $value
255 * @return string
256 */
257 public static function kebab($value)
258 {
259 return static::snake($value, '-');
260 }
261
262 /**
263 * Return the length of the given string.
264 *
265 * @param string $value
266 * @param string|null $encoding
267 * @return int
268 */
269 public static function length($value, $encoding = null)
270 {
271 if ($encoding) {
272 return mb_strlen($value, $encoding);
273 }
274
275 return mb_strlen($value);
276 }
277
278 /**
279 * Limit the number of characters in a string.
280 *
281 * @param string $value
282 * @param int $limit
283 * @param string $end
284 * @return string
285 */
286 public static function limit($value, $limit = 100, $end = '...')
287 {
288 if (mb_strwidth($value, 'UTF-8') <= $limit) {
289 return $value;
290 }
291
292 return rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')).$end;
293 }
294
295 /**
296 * Convert the given string to lower-case.
297 *
298 * @param string $value
299 * @return string
300 */
301 public static function lower($value)
302 {
303 return mb_strtolower($value, 'UTF-8');
304 }
305
306 /**
307 * Limit the number of words in a string.
308 *
309 * @param string $value
310 * @param int $words
311 * @param string $end
312 * @return string
313 */
314 public static function words($value, $words = 100, $end = '...')
315 {
316 preg_match('/^\s*+(?:\S++\s*+){1,'.$words.'}/u', $value, $matches);
317
318 if (! isset($matches[0]) || static::length($value) === static::length($matches[0])) {
319 return $value;
320 }
321
322 return rtrim($matches[0]).$end;
323 }
324
325 /**
326 * Masks a portion of a string with a repeated character.
327 *
328 * @param string $string
329 * @param string $character
330 * @param int $index
331 * @param int|null $length
332 * @param string $encoding
333 * @return string
334 */
335 public static function mask($string, $character, $index, $length = null, $encoding = 'UTF-8')
336 {
337 if ($character === '') {
338 return $string;
339 }
340
341 if (is_null($length) && PHP_MAJOR_VERSION < 8) {
342 $length = mb_strlen($string, $encoding);
343 }
344
345 $segment = mb_substr($string, $index, $length, $encoding);
346
347 if ($segment === '') {
348 return $string;
349 }
350
351 $start = mb_substr($string, 0, mb_strpos($string, $segment, 0, $encoding), $encoding);
352 $end = mb_substr($string, mb_strpos($string, $segment, 0, $encoding) + mb_strlen($segment, $encoding));
353
354 return $start.str_repeat(mb_substr($character, 0, 1, $encoding), mb_strlen($segment, $encoding)).$end;
355 }
356
357 /**
358 * Get the string matching the given pattern.
359 *
360 * @param string $pattern
361 * @param string $subject
362 * @return string
363 */
364 public static function match($pattern, $subject)
365 {
366 preg_match($pattern, $subject, $matches);
367
368 if (! $matches) {
369 return '';
370 }
371
372 return isset($matches[1]) ? $matches[1] : $matches[0];
373 }
374
375 /**
376 * Pad both sides of a string with another.
377 *
378 * @param string $value
379 * @param int $length
380 * @param string $pad
381 * @return string
382 */
383 public static function padBoth($value, $length, $pad = ' ')
384 {
385 return str_pad($value, $length, $pad, STR_PAD_BOTH);
386 }
387
388 /**
389 * Pad the left side of a string with another.
390 *
391 * @param string $value
392 * @param int $length
393 * @param string $pad
394 * @return string
395 */
396 public static function padLeft($value, $length, $pad = ' ')
397 {
398 return str_pad($value, $length, $pad, STR_PAD_LEFT);
399 }
400
401 /**
402 * Pad the right side of a string with another.
403 *
404 * @param string $value
405 * @param int $length
406 * @param string $pad
407 * @return string
408 */
409 public static function padRight($value, $length, $pad = ' ')
410 {
411 return str_pad($value, $length, $pad, STR_PAD_RIGHT);
412 }
413
414 /**
415 * Parse a Class[@]method style callback into class and method.
416 *
417 * @param string $callback
418 * @param string|null $default
419 * @return array<int, string|null>
420 */
421 public static function parseCallback($callback, $default = null)
422 {
423 return static::contains($callback, '@') ? explode('@', $callback, 2) : [$callback, $default];
424 }
425
426 /**
427 * Generate a more truly "random" alpha-numeric string.
428 *
429 * @param int $length
430 * @return string
431 */
432 public static function random($length = 16)
433 {
434 $string = '';
435
436 while (($len = strlen($string)) < $length) {
437 $size = $length - $len;
438
439 $bytes = random_bytes($size);
440
441 $string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size);
442 }
443
444 return $string;
445 }
446
447 /**
448 * Repeat the given string.
449 *
450 * @param string $string
451 * @param int $times
452 * @return string
453 */
454 public static function repeat($string, $times)
455 {
456 return str_repeat($string, $times);
457 }
458
459 /**
460 * Replace a given value in the string sequentially with an array.
461 *
462 * @param string $search
463 * @param array<int|string, string> $replace
464 * @param string $subject
465 * @return string
466 */
467 public static function replaceArray($search, array $replace, $subject)
468 {
469 $segments = explode($search, $subject);
470
471 $result = array_shift($segments);
472
473 foreach ($segments as $segment) {
474 $value = array_shift($replace);
475 $result .= ($value !== null ? $value : $search) . $segment;
476 }
477
478 return $result;
479 }
480
481 /**
482 * Replace the given value in the given string.
483 *
484 * @param string|string[] $search
485 * @param string|string[] $replace
486 * @param string|string[] $subject
487 * @return string
488 */
489 public static function replace($search, $replace, $subject)
490 {
491 return str_replace($search, $replace, $subject);
492 }
493
494 /**
495 * Replace the first occurrence of a given value in the string.
496 *
497 * @param string $search
498 * @param string $replace
499 * @param string $subject
500 * @return string
501 */
502 public static function replaceFirst($search, $replace, $subject)
503 {
504 $search = (string) $search;
505
506 if ($search === '') {
507 return $subject;
508 }
509
510 $position = strpos($subject, $search);
511
512 if ($position !== false) {
513 return substr_replace($subject, $replace, $position, strlen($search));
514 }
515
516 return $subject;
517 }
518
519 /**
520 * Replace the last occurrence of a given value in the string.
521 *
522 * @param string $search
523 * @param string $replace
524 * @param string $subject
525 * @return string
526 */
527 public static function replaceLast($search, $replace, $subject)
528 {
529 if ($search === '') {
530 return $subject;
531 }
532
533 $position = strrpos($subject, $search);
534
535 if ($position !== false) {
536 return substr_replace($subject, $replace, $position, strlen($search));
537 }
538
539 return $subject;
540 }
541
542 /**
543 * Remove any occurrence of the given string in the subject.
544 *
545 * @param string|array<string> $search
546 * @param string $subject
547 * @param bool $caseSensitive
548 * @return string
549 */
550 public static function remove($search, $subject, $caseSensitive = true)
551 {
552 return $caseSensitive
553 ? str_replace($search, '', $subject)
554 : str_ireplace($search, '', $subject);
555 }
556
557 /**
558 * Reverse the given string.
559 *
560 * @param string $value
561 * @return string
562 */
563 public static function reverse($value)
564 {
565 return implode(array_reverse(mb_str_split($value)));
566 }
567
568 /**
569 * Begin a string with a single instance of a given value.
570 *
571 * @param string $value
572 * @param string $prefix
573 * @return string
574 */
575 public static function start($value, $prefix)
576 {
577 $quoted = preg_quote($prefix, '/');
578
579 return $prefix.preg_replace('/^(?:'.$quoted.')+/u', '', $value);
580 }
581
582 /**
583 * Convert the given string to upper-case.
584 *
585 * @param string $value
586 * @return string
587 */
588 public static function upper($value)
589 {
590 return mb_strtoupper($value, 'UTF-8');
591 }
592
593 /**
594 * Convert the given string to title case.
595 *
596 * @param string $value
597 * @return string
598 */
599 public static function title($value)
600 {
601 return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8');
602 }
603
604 /**
605 * Convert the given string to title case for each word.
606 *
607 * @param string $value
608 * @return string
609 */
610 public static function headline($value)
611 {
612 $parts = explode(' ', $value);
613
614 $parts = count($parts) > 1
615 ? array_map([static::class, 'title'], $parts)
616 : array_map([static::class, 'title'], static::ucsplit(implode('_', $parts)));
617
618 $collapsed = static::replace(['-', '_', ' '], '_', implode('_', $parts));
619
620 return implode(' ', array_filter(explode('_', $collapsed)));
621 }
622
623 /**
624 * Generate a URL friendly "slug" from a given string.
625 *
626 * @param string $title
627 * @param string $fallback_title
628 * @param string $context
629 *
630 * @return string
631 */
632 public static function slug($title, $fallback_title = '', $context = 'save')
633 {
634 return sanitize_title($title, $fallback_title, $context);
635 }
636
637 /**
638 * Convert a string to snake case.
639 *
640 * @param string $value
641 * @param string $delimiter
642 * @return string
643 */
644 public static function snake($value, $delimiter = '_')
645 {
646 $key = $value;
647
648 if (isset(static::$snakeCache[$key][$delimiter])) {
649 return static::$snakeCache[$key][$delimiter];
650 }
651
652 if (! ctype_lower($value)) {
653 $value = preg_replace('/\s+/u', '', ucwords($value));
654
655 $value = static::lower(preg_replace('/(.)(?=[A-Z])/u', '$1'.$delimiter, $value));
656 }
657
658 return static::$snakeCache[$key][$delimiter] = $value;
659 }
660
661 /**
662 * Determine if a given string starts with a given substring.
663 *
664 * @param string $haystack
665 * @param string|string[] $needles
666 * @return bool
667 */
668 public static function startsWith($haystack, $needles)
669 {
670 foreach ((array) $needles as $needle) {
671 if ((string) $needle !== '' && str_starts_with($haystack, $needle)) {
672 return true;
673 }
674 }
675
676 return false;
677 }
678
679 /**
680 * Convert a value to studly caps case.
681 *
682 * @param string $value
683 * @return string
684 */
685 public static function studly($value)
686 {
687 $key = $value;
688
689 if (isset(static::$studlyCache[$key])) {
690 return static::$studlyCache[$key];
691 }
692
693 $words = explode(' ', static::replace(['-', '_'], ' ', $value));
694
695 $studlyWords = array_map(static function ($word) {
696 return static::ucfirst($word);
697 }, $words);
698
699 return static::$studlyCache[$key] = implode($studlyWords);
700 }
701
702 /**
703 * Returns the portion of the string specified by the start and length parameters.
704 *
705 * @param string $string
706 * @param int $start
707 * @param int|null $length
708 * @return string
709 */
710 public static function substr($string, $start, $length = null)
711 {
712 return mb_substr($string, $start, $length, 'UTF-8');
713 }
714
715 /**
716 * Returns the number of substring occurrences.
717 *
718 * @param string $haystack
719 * @param string $needle
720 * @param int $offset
721 * @param int|null $length
722 * @return int
723 */
724 public static function substrCount($haystack, $needle, $offset = 0, $length = null)
725 {
726 if (! is_null($length)) {
727 return substr_count($haystack, $needle, $offset, $length);
728 }
729
730 return substr_count($haystack, $needle, $offset);
731 }
732
733 /**
734 * Replace text within a portion of a string.
735 *
736 * @param string|array $string
737 * @param string|array $replace
738 * @param array|int $offset
739 * @param array|int|null $length
740 * @return string|array
741 */
742 public static function substrReplace($string, $replace, $offset = 0, $length = null)
743 {
744 if ($length === null) {
745 $length = strlen($string);
746 }
747
748 return substr_replace($string, $replace, $offset, $length);
749 }
750
751 /**
752 * Swap multiple keywords in a string with other keywords.
753 *
754 * @param array $map
755 * @param string $subject
756 * @return string
757 */
758 public static function swap(array $map, $subject)
759 {
760 return strtr($subject, $map);
761 }
762
763 /**
764 * Make a string's first character lowercase.
765 *
766 * @param string $string
767 * @return string
768 */
769 public static function lcfirst($string)
770 {
771 return static::lower(static::substr($string, 0, 1)).static::substr($string, 1);
772 }
773
774 /**
775 * Make a string's first character uppercase.
776 *
777 * @param string $string
778 * @return string
779 */
780 public static function ucfirst($string)
781 {
782 return static::upper(static::substr($string, 0, 1)).static::substr($string, 1);
783 }
784
785 /**
786 * Split a string into pieces by uppercase characters.
787 *
788 * @param string $string
789 * @return array
790 */
791 public static function ucsplit($string)
792 {
793 return preg_split('/(?=\p{Lu})/u', $string, -1, PREG_SPLIT_NO_EMPTY);
794 }
795
796 /**
797 * Get the number of words a string contains.
798 *
799 * @param string $string
800 * @return int
801 */
802 public static function wordCount($string)
803 {
804 return str_word_count($string);
805 }
806
807 /**
808 * Remove all strings from the casing caches.
809 *
810 * @return void
811 */
812 public static function flushCache()
813 {
814 static::$snakeCache = [];
815 static::$camelCache = [];
816 static::$studlyCache = [];
817 }
818 }
819