3v4l.org

run code in 500+ 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);
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.31, 8.3.0 - 8.3.31, 8.4.1 - 8.4.22, 8.5.0 - 8.5.7
Hello world, what do you think of today's beautiful weather

preferences:
81.47 ms | 1135 KiB | 4 Q