| @@ 644-674 (lines=31) @@ | ||
| 641 | } |
|
| 642 | } |
|
| 643 | ||
| 644 | if (!function_exists('str_replace_once')) { |
|
| 645 | /** |
|
| 646 | * Replace the first occurrence only. |
|
| 647 | * |
|
| 648 | * ### Example: |
|
| 649 | * |
|
| 650 | * ```php |
|
| 651 | * echo str_replace_once('A', 'a', 'AAABBBCCC'); |
|
| 652 | * // out: aAABBBCCC |
|
| 653 | * ``` |
|
| 654 | * |
|
| 655 | * @param string|array $search The value being searched for |
|
| 656 | * @param string $replace The replacement value that replaces found search value |
|
| 657 | * @param string $subject The string being searched and replaced on |
|
| 658 | * @return string A string with the replaced value |
|
| 659 | */ |
|
| 660 | function str_replace_once($search, $replace, $subject) |
|
| 661 | { |
|
| 662 | if (!is_array($search)) { |
|
| 663 | $search = [$search]; |
|
| 664 | } |
|
| 665 | ||
| 666 | foreach ($search as $s) { |
|
| 667 | if ($s !== '' && strpos($subject, $s) !== false) { |
|
| 668 | return substr_replace($subject, $replace, strpos($subject, $s), strlen($s)); |
|
| 669 | } |
|
| 670 | } |
|
| 671 | ||
| 672 | return $subject; |
|
| 673 | } |
|
| 674 | } |
|
| 675 | ||
| 676 | if (!function_exists('str_replace_last')) { |
|
| 677 | /** |
|
| @@ 676-706 (lines=31) @@ | ||
| 673 | } |
|
| 674 | } |
|
| 675 | ||
| 676 | if (!function_exists('str_replace_last')) { |
|
| 677 | /** |
|
| 678 | * Replace the last occurrence only. |
|
| 679 | * |
|
| 680 | * ### Example: |
|
| 681 | * |
|
| 682 | * ```php |
|
| 683 | * echo str_replace_once('A', 'a', 'AAABBBCCC'); |
|
| 684 | * // out: AAaBBBCCC |
|
| 685 | * ``` |
|
| 686 | * |
|
| 687 | * @param string|array $search The value being searched for |
|
| 688 | * @param string $replace The replacement value that replaces found search value |
|
| 689 | * @param string $subject The string being searched and replaced on |
|
| 690 | * @return string A string with the replaced value |
|
| 691 | */ |
|
| 692 | function str_replace_last($search, $replace, $subject) |
|
| 693 | { |
|
| 694 | if (!is_array($search)) { |
|
| 695 | $search = [$search]; |
|
| 696 | } |
|
| 697 | ||
| 698 | foreach ($search as $s) { |
|
| 699 | if ($s !== '' && strrpos($subject, $s) !== false) { |
|
| 700 | $subject = substr_replace($subject, $replace, strrpos($subject, $s), strlen($s)); |
|
| 701 | } |
|
| 702 | } |
|
| 703 | ||
| 704 | return $subject; |
|
| 705 | } |
|
| 706 | } |
|
| 707 | ||
| 708 | if (!function_exists('str_starts_with')) { |
|
| 709 | /** |
|