3v4l.org

run code in 300+ PHP versions simultaneously
<?php $originalString = "Hello world, what do you think of today's weather"; $findString = ' '; $nthOccurrence = 8; $newWord = 'beautiful '; function insertAfterNth($input, $find, $newString, $nth = 1) { $parts = explode($find, $input, $nth + 1); $parts[$nth] = $newString . $parts[$nth]; return implode($find, $parts); } echo insertAfterNth($originalString, $findString, $newWord, $nthOccurrence);

preferences:
28.75 ms | 405 KiB | 5 Q