// $str - text string, $len - wishful length, $sfx - suffix (for example: '...')
function humtrim($str, $len, $sfx)
{
if(strlen($str)>$len)
{
$len = $len - strlen($sfx) - 1;
preg_match("/^.{0,$len}\x20/", $str, $matches);
$str = $matches[0] . $sfx;
}
return $str;
}