original_string: public static function truncateWords($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 words.
          $words = new DOMWordsIterator($container);
          $truncated = false;
          foreach ($words as $word) {
  
              // If we have exceeded the limit, we delete the remainder of the content.
              if ($words->key() >= $limit) {
  
                  // Grab current position.
                  $currentWordPosition = $words->currentWordPosition();
                  $curNode = $currentWordPosition[0];
                  $offset = $currentWordPosition[1];
                  $words = $currentWordPosition[2];
  
                  $curNode->nodeValue = substr(
                      $curNode->nodeValue,
                      0,
                      $words[$offset][1] + strlen($words[$offset][0])
                  );
  
                  self::removeProceedingNodes($curNode, $container);
  
                  if (!empty($ellipsis)) {
                      self::insertEllipsis($curNode, $ellipsis);
                  }
  
                  $truncated = true;
  
                  break;
              }
  
          }
  
          // Return original HTML if not truncated.
          if ($truncated) {
              $html = self::getCleanedHtml($doc, $container);
          }
  
          return $html;
      }
code: public static function truncateWords($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 words.
          $words = new DOMWordsIterator($container);
          $truncated = false;
          foreach ($words as $word) {
  
              // If we have exceeded the limit, we delete the remainder of the content.
              if ($words->key() >= $limit) {
  
                  // Grab current position.
                  $currentWordPosition = $words->currentWordPosition();
                  $curNode = $currentWordPosition[0];
                  $offset = $currentWordPosition[1];
                  $words = $currentWordPosition[2];
  
                  $curNode->nodeValue = substr(
                      $curNode->nodeValue,
                      0,
                      $words[$offset][1] + strlen($words[$offset][0])
                  );
  
                  self::removeProceedingNodes($curNode, $container);
  
                  if (!empty($ellipsis)) {
                      self::insertEllipsis($curNode, $ellipsis);
                  }
  
                  $truncated = true;
  
                  break;
              }
  
          }
  
          // Return original HTML if not truncated.
          if ($truncated) {
              $html = self::getCleanedHtml($doc, $container);
          }
  
          return $html;
      }
code_tokens: public static function truncateWords ( $ 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 words. $ words = new DOMWordsIterator ( $ container ) ; $ truncated = false ; foreach ( $ words as $ word ) { // If we have exceeded the limit, we delete the remainder of the content. if ( $ words -> key ( ) >= $ limit ) { // Grab current position. $ currentWordPosition = $ words -> currentWordPosition ( ) ; $ curNode = $ currentWordPosition [ 0 ] ; $ offset = $ currentWordPosition [ 1 ] ; $ words = $ currentWordPosition [ 2 ] ; $ curNode -> nodeValue = substr ( $ curNode -> nodeValue , 0 , $ words [ $ offset ] [ 1 ] + strlen ( $ words [ $ offset ] [ 0 ] ) ) ; self :: removeProceedingNodes ( $ curNode , $ container ) ; if ( ! empty ( $ ellipsis ) ) { self :: insertEllipsis ( $ curNode , $ ellipsis ) ; } $ truncated = true ; break ; } } // Return original HTML if not truncated. if ( $ truncated ) { $ html = self :: getCleanedHtml ( $ doc , $ container ) ; } return $ html ; }
docstring: Safely truncates HTML by a given number of words.
  @param  string  $html     Input HTML.
  @param  int     $limit    Limit to how many words we preserve.
  @param  string  $ellipsis String to use as ellipsis (if any).
  @return string            Safe truncated HTML.
docstring_tokens: Safely truncates HTML by a given number of words .