- var_dump: documentation ( source)
- array_reverse: documentation ( source)
- array_search: documentation ( source)
- implode: documentation ( source)
- explode: documentation ( source)
<?php
var_dump(remove_last('bla test bla test bla test bla test bla', 'test', ' '));
function remove_last(string $text, string $subject, string $separator): string {
$parts = explode($separator, $text);
$pos = count($parts) -1 -array_search($subject, array_reverse($parts));
if ($parts[$pos] ?? false) {
unset($parts[$pos]);
}
return implode($separator, $parts);
}