Often times you want to display the name of something in a list (say, items in a store) but have limited space.
You can simply truncate strings that are longer than x characters and add an ellipsis, but occasionally you end up with:
"The quick brown fox ..." or "The quick brown f...". When "The quick brown fox..." or "The quick brown..." (respectively) would be nicer.
In PHP Code:
function pretty_substr($string, $length, $slop = 4) {
if (strlen($string) > $length) {
$string = substr($string, 0, $length - 1);
$lastSpace = strrpos($string, ' ');
if ($lastSpace > $length - $slop) {
$string = substr($string, 0, $lastSpace);
}
$string .= '&hellip;';
}
return $string;
}
I think this would make a good addition to the string function library.