By Etanol on
I have lots of content with long teasers that I am pretty happy about (I do not want to modify it), but I would like it to be displayed by one of the views truncated at a certain length.
To be more specific I would like the displayed teaser to be trimmed at last space before x chars.
I know I can
function truncateTeaser ($teaser, $maxLength)
{
$truncatePoint = strrpos($teaser, ' ', $maxLength);
if (($truncatePoint <= 0) || ($truncatePoint >$maxLength))
{
$truncatePoint = $maxLength;
}
return substr($teaser, 0, $truncatePoint);
}
possibly with some extra preprocessing if strrpos / substr offsets are not utf8-aware
But isn't there a better way?
Can someone point me in the right direction?