I have a code snippet written in PHP that pulls a block of text from a database and sends it out to a widget on a webpage. The original block of text can be a lengthy artic
I find this works:
function abbreviate_string_to_whole_word($string,$max_length,$buffer) {
if (strlen($string)>$max_length) {
$string_cropped=substr($string,0,$max_length-$buffer);
$last_space=strrpos($string_cropped, " ");
if ($last_space>0) {
$string_cropped=substr($string_cropped,0,$last_space);
}
$abbreviated_string=$string_cropped." ...";
}
else {
$abbreviated_string=$string;
}
return $abbreviated_string;
}
The buffer allows you to adjust the length of the returned string.