First note the changes to my first post. I made a few changes to the function, see the edit msg.
Hi Weedpacket, if your going to go with that route why do the first couple of if statements?
string str_shorten ( string text, int max_length[, string ending])
Function operation: If the string is already shorter than what is to be cut, just return the string. Else, cut the string to the max length. Next, check to see if the cut string ended or ends with a space, return cut str if so. Else, try and find the last space. If found, shorten the cut string to the space. Else, just return the cut string (user is expecting the string be shorter than the max length).
function str_shorten($str, $len, $end='...')
{
if ($len < strlen($str)) {
$cut = substr($str, 0, $len);
if ($str{$len} == ' ')
$short = $cut . $end;
elseif ($str{$len-1} == ' ')
$short = rtrim($cut) . $end;
elseif (($space = strrpos($cut, ' ')) !== false)
$short = substr($cut, 0, $space) . $end;
else
$short = $cut . $end;
} else
$short = $str;
return($short);
}