public static function truncate($str, $len = 10, $middle = true)
{
// make sure $len is a positive integer
if (!is_numeric($len) || 0 > $len) {
return $str;
}
// if the string is less than the length specified, bail out
if (MultiByte::strlen($str) <= $len) {
return $str;
}
// okay. Shuold we place the ellipse in the middle?
if ($middle) {
// yes, so compute the size of each half of the string
$len = round(($len - 3) / 2);
// and place an ellipse in between the pieces
return MultiByte::substr($str, 0, $len) . '…' . MultiByte::substr($str, -$len);
} else {
// no, the ellipse goes at the end
$len = $len - 3;
return MultiByte::substr($str, 0, $len) . '…';
}
}