PHP - Function to replace nth occurence of a string without preg_match or preg_replace. In both 5.6 and 7.0 -- 2
$10-30 USD
Completed
Posted about 4 years ago
$10-30 USD
Paid on delivery
I need a PHP function that can to a str_replace on specified occurrence of the needle in the haystack.
So for example
function replace_nth(find, replace, haystack, position).
function replace_nth('the', 'a', 'the angry man jumped over the gate and ate the sandwich', 2) {
}
should return "the angry man jumped over a gate and ate the sandwich".
the function shouldn't use any preg_match or preg_replace functions. It should also not rely on any strlen() function.
I need a version that works in PHP 5.6 and a version that works in PHP7 (if there is a better one that is using PHP 7 functions.
Thanks