You're close. Just use array_pop()
instead of array_shift()
. array_pop()
removes last element of array. You need, of course, use implode()
later to put the strign together again.
$arr = explode('-', $str);
array_pop($arr);
$str = implode('-', $arr);
It's important not to do that in one line since array_pop()
works on a reference to the array and it modfies it, and then returns only removed element.
There are a few other possible solutions mentions by other answers.