SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru
    Join Date
    Mar 2004
    Posts
    639
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to remove last word from string?

    Hello guys,

    I'm cutting first 400 symbols from the text and I'd like to remove from the result string last word. I wonder is there built-in PHP function that will cut all letters until " " (space) symbol starting from the end of the string?

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $b substr($a0strrpos($a" ")); 
    or
    PHP Code:
    $b preg_replace('~\s+\S+$~'''$a); 
    will remove the last "word". It would be perhaps simpler just to cut first, say, 40 words instead of 400 symbols. Search the forums, this has been discussed many times before.

  3. #3
    SitePoint Guru
    Join Date
    Mar 2004
    Posts
    639
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog View Post
    PHP Code:
    $b substr($a0strrpos($a" ")); 
    or
    PHP Code:
    $b preg_replace('~\s+\S+$~'''$a); 
    will remove the last "word". It would be perhaps simpler just to cut first, say, 40 words instead of 400 symbols. Search the forums, this has been discussed many times before.
    Thank you very much for advice.

    strrpos() works perfectly.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •