3v4l.org

run code in 300+ PHP versions simultaneously
<?php function cutAtNthOf(string $text, string $char, int $nth): string { $result = ''; for ($i = 0; $i < $nth; ++$i) { $result .= $i ? $char . strtok($char) : strtok($text, $char); } return $result; } $test = 'hey/mate/from/outside//nothing'; for ($nth = 0; $nth <= 7; ++$nth) { echo cutAtNthOf($test, '/', $nth) . "\n"; } echo "\n---\n"; $test = 'foo/////////bar'; $one = strtok($test, '/'); $two = strtok('/'); echo $one . '/' . $two;
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/nothing hey/mate/from/outside/nothing/ hey/mate/from/outside/nothing// --- foo/bar

preferences:
79.72 ms | 402 KiB | 62 Q