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);
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.27, 8.4.1 - 8.4.14
Hello world, what do you think of today's beautiful weather

preferences:
94.37 ms | 406 KiB | 5 Q