Проблемы могут возникнуть, если ваша строка имеет HTML-теги, & nbsp и несколько пробелов. Вот что я использую, чтобы позаботиться обо всем:
function LimitText($string,$limit,$remove_html=0){
if ($remove_html==1){$string=strip_tags($string);}
$newstring = preg_replace("/(?:\s| )+/"," ",$string, -1); // replace   with space
$newstring = preg_replace(array('/\s{2,}/','/[\t\n]/'),' ',$newstring); // replace duplicate spaces
if (strlen($newstring)<=$limit) { return $newstring; } // ensure length is more than $limit
$newstring = substr($newstring,0,strrpos(substr($newstring,0,$limit),' '));
return $newstring;
}
использование:
$string = 'My wife is jealous of stackoverflow';
echo LimitText($string,20);
// My wife is jealous
использование с html:
$string = '<div><p>My wife is jealous of stackoverflow</p></div>';
echo LimitText($string,20,1);
// My wife is jealous