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:

$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

$truncated = substr($longstring,0,strpos($longstring,' ',100));

with

$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?

This is a job for a Regular Expression.
A pattern like this will do it (I think):


/[. \
]$/

Is this text originally coming from your database?