src/Graby.php
private function getExcerpt($text, $length = 250, $separator = ' &hellip;')
    {
        // use regex instead of strip_tags to left some spaces when removing tags
        $text = preg_replace('#<[^>]+>#', ' ', $text);

        // trim whitespace at beginning or end of string
        // See: http://stackoverflow.com/a/4167053/569101
        $text = preg_replace('/^[\pZ\pC]+|[\pZ\pC]+$/u', '', $text);
        // clean new lines and tabs
        $text = trim(preg_replace("/[\n\r\t ]+/", ' ', $text), ' ');

        if (mb_strlen($text) > $length) {
            // If breakpoint is on the last word, return the text without separator.
            if (false === ($breakpoint = mb_strpos($text, ' ', $length))) {
                return $text;
            }
            $length = $breakpoint;

            return rtrim(mb_substr($text, 0, $length)) . $separator;
        }

        return $text;
    }
private function getExcerpt($text, $length = 250, $separator = ' &hellip;')
    {
        // use regex instead of strip_tags to left some spaces when removing tags
        $text = preg_replace('#<[^>]+>#', ' ', $text);

        // trim whitespace at beginning or end of string
        // See: http://stackoverflow.com/a/4167053/569101
        $text = preg_replace('/^[\pZ\pC]+|[\pZ\pC]+$/u', '', $text);
        // clean new lines and tabs
        $text = trim(preg_replace("/[\n\r\t ]+/", ' ', $text), ' ');

        if (mb_strlen($text) > $length) {
            // If breakpoint is on the last word, return the text without separator.
            if (false === ($breakpoint = mb_strpos($text, ' ', $length))) {
                return $text;
            }
            $length = $breakpoint;

            return rtrim(mb_substr($text, 0, $length)) . $separator;
        }

        return $text;
    }
private function getExcerpt ( $ text , $ length = 250 , $ separator = ' &hellip;' ) { // use regex instead of strip_tags to left some spaces when removing tags $ text = preg_replace ( '#<[^>]+>#' , ' ' , $ text ) ; // trim whitespace at beginning or end of string // See: http://stackoverflow.com/a/4167053/569101 $ text = preg_replace ( '/^[\pZ\pC]+|[\pZ\pC]+$/u' , '' , $ text ) ; // clean new lines and tabs $ text = trim ( preg_replace ( "/[\n\r\t ]+/" , ' ' , $ text ) , ' ' ) ; if ( mb_strlen ( $ text ) > $ length ) { // If breakpoint is on the last word, return the text without separator. if ( false === ( $ breakpoint = mb_strpos ( $ text , ' ' , $ length ) ) ) { return $ text ; } $ length = $ breakpoint ; return rtrim ( mb_substr ( $ text , 0 , $ length ) ) . $ separator ; } return $ text ; }
Truncate text.

@see https://github.com/twigphp/Twig-extensions/blob/449e3c8a9ffad7c2479c7864557275a32b037499/lib/Twig/Extensions/Extension/Text.php#L40

@param string $text
@param int    $length
@param string $separator

@return string
Truncate text .