This one will give you a little more flexibility
function string_limiter($string, $limit = 50, $end_char = '…')
{
if (trim($string) == '')
{
return $string;
}
preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/', $string, $matches);
if (strlen($string) == strlen($matches[0]))
{
$end_char = '';
}
return rtrim($matches[0]).$end_char;
}
Example how to call it
$str = "Cras id ipsum accumsan dolor pulvinar sollicitudin et eu augue. Pellentesque sem metus, imperdiet tempor fringilla quis, convallis quis nisl. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Etiam eget lorem ligula. Sed congue odio enim. Etiam aliquet condimentum pellentesque. Sed urna velit, egestas et dictum non, pulvinar quis dui! Cras blandit porttitor tortor nec eleifend. Nullam porttitor scelerisque nunc, id porttitor orci lobortis nec. Curabitur vestibulum molestie purus, eget convallis dolor luctus eu! Morbi id felis eu eros mattis congue non nec nisl.";
echo string_limiter($str, 20);
You will get something like this:
Cras id ipsum accumsan dolor pulvinar
sollicitudin et eu augue. Pellentesque
sem metus, imperdiet tempor fringilla
quis, convallis quis nisl.…