Making sure PHP substr finishes on a word not a character

Cool Hand Luke picture Cool Hand Luke · Aug 5, 2009 · Viewed 86.3k times · Source

I know how to use the substr function but this will happy end a string halfway through a word. I want the string to end at the end of a word how would I go about doing this? Would it involve regular expression? Any help very much appreciated.

This is what I have so far. Just the SubStr...

echo substr("$body",0,260);

Cheers

Answer

Achshar picture Achshar · Jun 2, 2012
substr($body, 0, strpos($body, ' ', 260))