|
6 | 6 |
|
7 | 7 | use function abs; |
8 | 8 | use function array_reverse; |
| 9 | +use function array_shift; |
9 | 10 | use function ctype_lower; |
10 | 11 | use function explode; |
11 | 12 | use function implode; |
|
21 | 22 | use function mb_strwidth; |
22 | 23 | use function mb_substr; |
23 | 24 | use function preg_match; |
| 25 | +use function preg_quote; |
24 | 26 | use function preg_replace; |
25 | 27 | use function random_int; |
26 | 28 | use function rtrim; |
27 | 29 | use function str_pad; |
28 | 30 | use function str_replace; |
| 31 | +use function strlen; |
| 32 | +use function strncmp; |
| 33 | +use function strpos; |
| 34 | +use function strrpos; |
| 35 | +use function substr; |
| 36 | +use function substr_replace; |
29 | 37 | use function trim; |
30 | 38 | use function ucwords; |
31 | 39 |
|
@@ -528,4 +536,124 @@ public static function stripSpaces(string $string): string |
528 | 536 | { |
529 | 537 | return preg_replace('/\s+/', '', $string); |
530 | 538 | } |
| 539 | + |
| 540 | + /** |
| 541 | + * Replace a given value in the string sequentially with an array. |
| 542 | + * |
| 543 | + * @param string $string String |
| 544 | + * @param string $search Search |
| 545 | + * @param array $replace Replace |
| 546 | + */ |
| 547 | + public static function replaceArray(string $string, string $search, array $replace): string |
| 548 | + { |
| 549 | + $segments = explode($search, $string); |
| 550 | + |
| 551 | + $result = array_shift($segments); |
| 552 | + |
| 553 | + foreach ($segments as $segment) { |
| 554 | + $result .= (array_shift($replace) ?? $search) . $segment; |
| 555 | + } |
| 556 | + |
| 557 | + return $result; |
| 558 | + } |
| 559 | + |
| 560 | + /** |
| 561 | + * Replace the first occurrence of a given value in the string. |
| 562 | + * |
| 563 | + * @param string $string String |
| 564 | + * @param string $search Search |
| 565 | + * @param string $replace Replace |
| 566 | + */ |
| 567 | + public static function replaceFirst(string $string, string $search, string $replace): string |
| 568 | + { |
| 569 | + if ($search === '') { |
| 570 | + return $string; |
| 571 | + } |
| 572 | + |
| 573 | + $position = strpos($string, $search); |
| 574 | + |
| 575 | + if ($position !== false) { |
| 576 | + return substr_replace($string, $replace, $position, strlen($search)); |
| 577 | + } |
| 578 | + |
| 579 | + return $search; |
| 580 | + } |
| 581 | + |
| 582 | + /** |
| 583 | + * Replace the last occurrence of a given value in the string. |
| 584 | + * |
| 585 | + * @param string $string String |
| 586 | + * @param string $search Search |
| 587 | + * @param string $replace Replace |
| 588 | + */ |
| 589 | + public static function replaceLast(string $string, string $search, string $replace): string |
| 590 | + { |
| 591 | + $position = strrpos($string, $search); |
| 592 | + |
| 593 | + if ($position !== false) { |
| 594 | + return substr_replace($string, $replace, $position, strlen($search)); |
| 595 | + } |
| 596 | + |
| 597 | + return $subject; |
| 598 | + } |
| 599 | + |
| 600 | + /** |
| 601 | + * Begin a string with a single instance of a given value. |
| 602 | + * |
| 603 | + * @param string $string String |
| 604 | + * @param string $prefix Prefix |
| 605 | + */ |
| 606 | + public static function start(string $string, string $prefix): string |
| 607 | + { |
| 608 | + $quoted = preg_quote($prefix, '/'); |
| 609 | + |
| 610 | + return $prefix . preg_replace('/^(?:' . $quoted . ')+/u', '', $string); |
| 611 | + } |
| 612 | + |
| 613 | + /** |
| 614 | + * Determine if a given string starts with a given substring. |
| 615 | + * |
| 616 | + * @param string $haystack Haystack |
| 617 | + * @param string|string[] $needles needles |
| 618 | + */ |
| 619 | + public static function startsWith(string $haystack, $needles): bool |
| 620 | + { |
| 621 | + foreach ((array) $needles as $needle) { |
| 622 | + if ((string) $needle !== '' && strncmp($haystack, $needle, strlen($needle)) === 0) { |
| 623 | + return true; |
| 624 | + } |
| 625 | + } |
| 626 | + |
| 627 | + return false; |
| 628 | + } |
| 629 | + |
| 630 | + /** |
| 631 | + * Determine if a given string ends with a given substring. |
| 632 | + * |
| 633 | + * @param string $haystack Haystack |
| 634 | + * @param string|string[] $needles needles |
| 635 | + */ |
| 636 | + public static function endsWith(string $haystack, $needles): bool |
| 637 | + { |
| 638 | + foreach ((array) $needles as $needle) { |
| 639 | + if ($needle !== '' && substr($haystack, -strlen($needle)) === (string) $needle) { |
| 640 | + return true; |
| 641 | + } |
| 642 | + } |
| 643 | + |
| 644 | + return false; |
| 645 | + } |
| 646 | + |
| 647 | + /** |
| 648 | + * Cap a string with a single instance of a given value. |
| 649 | + * |
| 650 | + * @param string $string String |
| 651 | + * @param string $cap Cap |
| 652 | + */ |
| 653 | + public static function finish(string $string, string $cap): string |
| 654 | + { |
| 655 | + $quoted = preg_quote($cap, '/'); |
| 656 | + |
| 657 | + return preg_replace('/(?:' . $quoted . ')+$/u', '', $string) . $cap; |
| 658 | + } |
531 | 659 | } |
0 commit comments