SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2010
    Posts
    167
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Truncate Text after last full sentence.

    Hi there,

    I have written a script that automatically chops off anything over 100 characters and I have the following line in there to make sure it does so at the last full word:

    PHP Code:
    $truncated substr($longstring,0,strpos($longstring,' ',100)); 
    This ensures that I'm not chopping in the middle of a word - It counts back to the last space before the 100th character.

    I want to change this so that it counts back to the last full stop before the 100th character so that I can enhance the script even more and truncate at the end of a sentence rather than the end of a word.

    I have tried replacing

    PHP Code:
    $truncated substr($longstring,0,strpos($longstring,' ',100)); 
    with

    PHP Code:
    $truncated substr($longstring,0,strpos($longstring,'.',100)); 
    but it doesn't work...

    How can I tell it to count back to the last full stop instead of the last space?

  2. #2
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,326
    Mentioned
    191 Post(s)
    Tagged
    4 Thread(s)
    This is a job for a Regular Expression.
    A pattern like this will do it (I think):
    Code:
    /[. \n]$/
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Is this text originally coming from your database?


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
  •