| @@ 623-653 (lines=31) @@ | ||
| 620 | } |
|
| 621 | } |
|
| 622 | ||
| 623 | if (!function_exists('str_replace_once')) { |
|
| 624 | /** |
|
| 625 | * Replace the first occurrence only. |
|
| 626 | * |
|
| 627 | * ### Example: |
|
| 628 | * |
|
| 629 | * ```php |
|
| 630 | * echo str_replace_once('A', 'a', 'AAABBBCCC'); |
|
| 631 | * // out: aAABBBCCC |
|
| 632 | * ``` |
|
| 633 | * |
|
| 634 | * @param string|array $search The value being searched for |
|
| 635 | * @param string $replace The replacement value that replaces found search value |
|
| 636 | * @param string $subject The string being searched and replaced on |
|
| 637 | * @return string A string with the replaced value |
|
| 638 | */ |
|
| 639 | function str_replace_once($search, $replace, $subject) |
|
| 640 | { |
|
| 641 | if (!is_array($search)) { |
|
| 642 | $search = [$search]; |
|
| 643 | } |
|
| 644 | ||
| 645 | foreach ($search as $s) { |
|
| 646 | if ($s !== '' && strpos($subject, $s) !== false) { |
|
| 647 | return substr_replace($subject, $replace, strpos($subject, $s), strlen($s)); |
|
| 648 | } |
|
| 649 | } |
|
| 650 | ||
| 651 | return $subject; |
|
| 652 | } |
|
| 653 | } |
|
| 654 | ||
| 655 | if (!function_exists('str_replace_last')) { |
|
| 656 | /** |
|
| @@ 655-685 (lines=31) @@ | ||
| 652 | } |
|
| 653 | } |
|
| 654 | ||
| 655 | if (!function_exists('str_replace_last')) { |
|
| 656 | /** |
|
| 657 | * Replace the last occurrence only. |
|
| 658 | * |
|
| 659 | * ### Example: |
|
| 660 | * |
|
| 661 | * ```php |
|
| 662 | * echo str_replace_once('A', 'a', 'AAABBBCCC'); |
|
| 663 | * // out: AAaBBBCCC |
|
| 664 | * ``` |
|
| 665 | * |
|
| 666 | * @param string|array $search The value being searched for |
|
| 667 | * @param string $replace The replacement value that replaces found search value |
|
| 668 | * @param string $subject The string being searched and replaced on |
|
| 669 | * @return string A string with the replaced value |
|
| 670 | */ |
|
| 671 | function str_replace_last($search, $replace, $subject) |
|
| 672 | { |
|
| 673 | if (!is_array($search)) { |
|
| 674 | $search = [$search]; |
|
| 675 | } |
|
| 676 | ||
| 677 | foreach ($search as $s) { |
|
| 678 | if ($s !== '' && strrpos($subject, $s) !== false) { |
|
| 679 | $subject = substr_replace($subject, $replace, strrpos($subject, $s), strlen($s)); |
|
| 680 | } |
|
| 681 | } |
|
| 682 | ||
| 683 | return $subject; |
|
| 684 | } |
|
| 685 | } |
|
| 686 | ||
| 687 | if (!function_exists('str_starts_with')) { |
|
| 688 | /** |
|