public static function truncateLetters($html, $limit = 0, $ellipsis = '')
    {
        if ($limit <= 0) {
            return $html;
        }

        $doc = self::htmlToDomDocument($html);
        $container = $doc->getElementsByTagName('div')->item(0);
        $container = $container->parentNode->removeChild($container);

        // Iterate over letters.
        $letters = new DOMLettersIterator($container);
        $truncated = false;
        foreach ($letters as $letter) {

            // If we have exceeded the limit, we want to delete the remainder of this document.
            if ($letters->key() >= $limit) {

                $currentText = $letters->currentTextPosition();
                $currentText[0]->nodeValue = mb_substr($currentText[0]->nodeValue, 0, $currentText[1] + 1);
                self::removeProceedingNodes($currentText[0], $container);

                if (!empty($ellipsis)) {
                    self::insertEllipsis($currentText[0], $ellipsis);
                }

                $truncated = true;

                break;
            }
        }

        // Return original HTML if not truncated.
        if ($truncated) {
            $html = self::getCleanedHtml($doc, $container);
        }

        return $html;
    }
public static function truncateLetters($html, $limit = 0, $ellipsis = '')
    {
        if ($limit <= 0) {
            return $html;
        }

        $doc = self::htmlToDomDocument($html);
        $container = $doc->getElementsByTagName('div')->item(0);
        $container = $container->parentNode->removeChild($container);

        // Iterate over letters.
        $letters = new DOMLettersIterator($container);
        $truncated = false;
        foreach ($letters as $letter) {

            // If we have exceeded the limit, we want to delete the remainder of this document.
            if ($letters->key() >= $limit) {

                $currentText = $letters->currentTextPosition();
                $currentText[0]->nodeValue = mb_substr($currentText[0]->nodeValue, 0, $currentText[1] + 1);
                self::removeProceedingNodes($currentText[0], $container);

                if (!empty($ellipsis)) {
                    self::insertEllipsis($currentText[0], $ellipsis);
                }

                $truncated = true;

                break;
            }
        }

        // Return original HTML if not truncated.
        if ($truncated) {
            $html = self::getCleanedHtml($doc, $container);
        }

        return $html;
    }
public static function truncateLetters ( $ html , $ limit = 0 , $ ellipsis = '' ) { if ( $ limit <= 0 ) { return $ html ; } $ doc = self :: htmlToDomDocument ( $ html ) ; $ container = $ doc -> getElementsByTagName ( 'div' ) -> item ( 0 ) ; $ container = $ container -> parentNode -> removeChild ( $ container ) ; // Iterate over letters. $ letters = new DOMLettersIterator ( $ container ) ; $ truncated = false ; foreach ( $ letters as $ letter ) { // If we have exceeded the limit, we want to delete the remainder of this document. if ( $ letters -> key ( ) >= $ limit ) { $ currentText = $ letters -> currentTextPosition ( ) ; $ currentText [ 0 ] -> nodeValue = mb_substr ( $ currentText [ 0 ] -> nodeValue , 0 , $ currentText [ 1 ] + 1 ) ; self :: removeProceedingNodes ( $ currentText [ 0 ] , $ container ) ; if ( ! empty ( $ ellipsis ) ) { self :: insertEllipsis ( $ currentText [ 0 ] , $ ellipsis ) ; } $ truncated = true ; break ; } } // Return original HTML if not truncated. if ( $ truncated ) { $ html = self :: getCleanedHtml ( $ doc , $ container ) ; } return $ html ; }
Safely truncates HTML by a given number of letters.
@param  string  $html     Input HTML.
@param  int     $limit    Limit to how many letters we preserve.
@param  string  $ellipsis String to use as ellipsis (if any).
@return string            Safe truncated HTML.
Safely truncates HTML by a given number of letters .