3v4l.org

run code in 300+ PHP versions simultaneously
<?php function cutAtNthOf(string $text, string $char, int $nth): string { $char = preg_quote($char); return preg_replace("#(?:[^$char]*\K$char){{$nth}}.*#", '', $text); } $test = 'hey/mate/from/outside//nothing'; for ($nth = 0; $nth <= 7; ++$nth) { echo cutAtNthOf($test, '/', $nth) . "\n"; }
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
hey hey/mate hey/mate/from hey/mate/from/outside hey/mate/from/outside/ hey/mate/from/outside//nothing hey/mate/from/outside//nothing

preferences:
78.71 ms | 402 KiB | 62 Q