BetaCategory: PHP - Owner: PaulDavis - Owner ID: 1
Trims the given tring by the word, rather than letter. Looks more 'natural'.
Adapted from: http://www.lullabot.com/articles/trim_a_string_to_a_given_word_count
function trimWords($string, $count, $ellipsis = false){
$words = explode(' ', $string);
if (count($words) > $count){
array_splice($words, $count);
$string = implode(' ', $words);
if (is_string($ellipsis)){
$string .= $ellipsis;
} elseif ($ellipsis){
$string .= '…';
}
}
return $string;
}