Find last word of a string


/ Published in: PHP
Save to your folder(s)

Returns last word of a string (words separated by blanks). if string does not have any blanks, it will return the whole string.


Copy this code and paste it in your HTML
  1. /*-------------------------------------------------------------------
  2. /* Submodul: last_word()
  3. /* Version: 1.0
  4. /* Beschr.: Gibt das letzte Wort eines Strings zurück
  5. /* IN: string
  6. /* OUT: wort (letztes Wort aus String)
  7. /*------------------------------------------------------------------- */
  8.  
  9. function last_word($string)
  10. {
  11. if ( strrpos($string, " ") ) {
  12. $letztes_wort_anfang = strrpos($string, " ") + 1;
  13. $laenge_letztes_wort = strlen($string) - $letztes_wort_anfang;
  14. $letztes_wort = substr($string, $letztes_wort_anfang, $laenge_letztes_wort);
  15. return $letztes_wort;
  16. }
  17. else
  18. return $string;
  19. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.