src/helpers/BaseHtmlPurifier.php
public static function truncateCharacters(string $content, int $count, string $suffix = '...', string $encoding = 'utf-8', $config = null): string
    {
        $config = static::createConfig($config);

        $tokens = \HTMLPurifier_Lexer::create($config)->tokenizeHTML($content, $config, new \HTMLPurifier_Context());
        $openTokens = [];
        $totalCount = 0;
        $depth = 0;
        $truncated = [];
        foreach ($tokens as $token) {
            if ($token instanceof \HTMLPurifier_Token_Start) { //Tag begins
                $openTokens[$depth] = $token->name;
                $truncated[] = $token;
                ++$depth;
            } elseif ($token instanceof \HTMLPurifier_Token_Text && $totalCount <= $count) { //Text
                $token->data = StringHelper::truncateCharacters($token->data, $count - $totalCount, '', $encoding);
                $currentCount = mb_strlen($token->data, $encoding);
                $totalCount += $currentCount;
                $truncated[] = $token;
            } elseif ($token instanceof \HTMLPurifier_Token_End) { //Tag ends
                if ($token->name === $openTokens[$depth - 1]) {
                    --$depth;
                    unset($openTokens[$depth]);
                    $truncated[] = $token;
                }
            } elseif ($token instanceof \HTMLPurifier_Token_Empty) { //Self contained tags, i.e. <img/> etc.
                $truncated[] = $token;
            }
            if ($totalCount >= $count) {
                if (0 < count($openTokens)) {
                    krsort($openTokens);
                    foreach ($openTokens as $name) {
                        $truncated[] = new \HTMLPurifier_Token_End($name);
                    }
                }
                break;
            }
        }
        $context = new \HTMLPurifier_Context();
        $generator = new \HTMLPurifier_Generator($config, $context);

        return $generator->generateFromTokens($truncated) . ($totalCount >= $count ? $suffix : '');
    }
public static function truncateCharacters(string $content, int $count, string $suffix = '...', string $encoding = 'utf-8', $config = null): string
    {
        $config = static::createConfig($config);

        $tokens = \HTMLPurifier_Lexer::create($config)->tokenizeHTML($content, $config, new \HTMLPurifier_Context());
        $openTokens = [];
        $totalCount = 0;
        $depth = 0;
        $truncated = [];
        foreach ($tokens as $token) {
            if ($token instanceof \HTMLPurifier_Token_Start) { //Tag begins
                $openTokens[$depth] = $token->name;
                $truncated[] = $token;
                ++$depth;
            } elseif ($token instanceof \HTMLPurifier_Token_Text && $totalCount <= $count) { //Text
                $token->data = StringHelper::truncateCharacters($token->data, $count - $totalCount, '', $encoding);
                $currentCount = mb_strlen($token->data, $encoding);
                $totalCount += $currentCount;
                $truncated[] = $token;
            } elseif ($token instanceof \HTMLPurifier_Token_End) { //Tag ends
                if ($token->name === $openTokens[$depth - 1]) {
                    --$depth;
                    unset($openTokens[$depth]);
                    $truncated[] = $token;
                }
            } elseif ($token instanceof \HTMLPurifier_Token_Empty) { //Self contained tags, i.e. <img/> etc.
                $truncated[] = $token;
            }
            if ($totalCount >= $count) {
                if (0 < count($openTokens)) {
                    krsort($openTokens);
                    foreach ($openTokens as $name) {
                        $truncated[] = new \HTMLPurifier_Token_End($name);
                    }
                }
                break;
            }
        }
        $context = new \HTMLPurifier_Context();
        $generator = new \HTMLPurifier_Generator($config, $context);

        return $generator->generateFromTokens($truncated) . ($totalCount >= $count ? $suffix : '');
    }
public static function truncateCharacters ( string $ content , int $ count , string $ suffix = '...' , string $ encoding = 'utf-8' , $ config = null ) : string { $ config = static :: createConfig ( $ config ) ; $ tokens = \ HTMLPurifier_Lexer :: create ( $ config ) -> tokenizeHTML ( $ content , $ config , new \ HTMLPurifier_Context ( ) ) ; $ openTokens = [ ] ; $ totalCount = 0 ; $ depth = 0 ; $ truncated = [ ] ; foreach ( $ tokens as $ token ) { if ( $ token instanceof \ HTMLPurifier_Token_Start ) { //Tag begins $ openTokens [ $ depth ] = $ token -> name ; $ truncated [ ] = $ token ; ++ $ depth ; } elseif ( $ token instanceof \ HTMLPurifier_Token_Text && $ totalCount <= $ count ) { //Text $ token -> data = StringHelper :: truncateCharacters ( $ token -> data , $ count - $ totalCount , '' , $ encoding ) ; $ currentCount = mb_strlen ( $ token -> data , $ encoding ) ; $ totalCount += $ currentCount ; $ truncated [ ] = $ token ; } elseif ( $ token instanceof \ HTMLPurifier_Token_End ) { //Tag ends if ( $ token -> name === $ openTokens [ $ depth - 1 ] ) { -- $ depth ; unset ( $ openTokens [ $ depth ] ) ; $ truncated [ ] = $ token ; } } elseif ( $ token instanceof \ HTMLPurifier_Token_Empty ) { //Self contained tags, i.e. <img/> etc. $ truncated [ ] = $ token ; } if ( $ totalCount >= $ count ) { if ( 0 < count ( $ openTokens ) ) { krsort ( $ openTokens ) ; foreach ( $ openTokens as $ name ) { $ truncated [ ] = new \ HTMLPurifier_Token_End ( $ name ) ; } } break ; } } $ context = new \ HTMLPurifier_Context ( ) ; $ generator = new \ HTMLPurifier_Generator ( $ config , $ context ) ; return $ generator -> generateFromTokens ( $ truncated ) . ( $ totalCount >= $ count ? $ suffix : '' ) ; }
Truncate a HTML string to count of characters specified.

@param string $content The string to be truncated.
@param int $count
@param string $suffix String to append to the end of the truncated string. Default is empty string.
@param string $encoding
@param array|\Closure|null $config The config to use for HtmlPurifier.
@return string
@throws InvalidConfigException
@throws \HTMLPurifier_Exception
Truncate a HTML string to count of characters specified .