[php] Get first 100 characters from string, respecting full words

I have asked a similar question here before, but I need to know if this little tweak is possible. I want to shorten a string to 100 characters and use $small = substr($big, 0, 100); to do so. However, this just takes the first 100 characters and doesn't care whether it breaks up a word or not.

Is there any way to take up to the first 100 characters of a string but make sure you don't break a word?

Example:

$big = "This is a sentence that has more than 100 characters in it, and I want to return a string of only full words that is no more than 100 characters!"

$small = some_function($big);

echo $small;

// OUTPUT: "This is a sentence that has more than 100 characters in it, and I want to return a string of only"

Is there a way to do this using PHP?

This question is related to php string

The answer is


All you need to do is use:

$pos=strpos($content, ' ', 200);
substr($content,0,$pos ); 

Similar questions with php tag:

Similar questions with string tag: