-1

How can I remove all words from a string except the last one with PHP?

For example the string "Organic Black Olives" would become "Olives".

evavienna
  • 1,059
  • 11
  • 29

1 Answers1

1

Try this:

$str = "Organic Black Olives";
// get words list
$words = explode(' ', $str);
// get last word
$last_word = $words[count($words)-1];

echo $last_word;
Majed Badawi
  • 27,616
  • 4
  • 25
  • 48