Posted By

fischkopp on 03/20/09


Tagged

String function strings last word Wort Letztes


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

rezzz
BrianCoyDesign


Find last word of a string


 / Published in: PHP
 

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

  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  

You need to login to post a comment.