Habari\Utils::truncate PHP Méthode

truncate() public static méthode

Trims longer phrases to shorter ones with elipsis in the middle
public static truncate ( string $str, integer $len = 10, boolean $middle = true ) : string
$str string The string to truncate
$len integer The length of the returned string
$middle boolean Whether to place the ellipsis in the middle (true) or at the end (false)
Résultat string The truncated string
    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) . '&hellip;' . MultiByte::substr($str, -$len);
        } else {
            // no, the ellipse goes at the end
            $len = $len - 3;
            return MultiByte::substr($str, 0, $len) . '&hellip;';
        }
    }