<?php
function cutAtNthOf(string $text, string $char, int $nth): string
{
return implode(
$char,
array_slice(
explode(
$char,
$text,
$nth + 1
),
0,
$nth
)
);
}
$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:
51.43 ms | 402 KiB | 62 Q