Shortens string to given length,

@param string   $text
@param int|null $charCount
@param bool     $fullWords
@param string   $suffix
@return string
960849
public static function shorten(string $text = '', ?int $charCount = null, bool $fullWords = true,
                                   string $suffix = '...'): string
    {
        $text = StripTags::strip($text);
        $text = \htmlspecialchars_decode((string)$text, ENT_COMPAT | ENT_HTML5);
        $text = \trim(\preg_replace('/\s+/', ' ', $text));

        if ($charCount === null || \mb_strlen($text, 'UTF-8') <= $charCount) {
            return $text;
        }

        if ($fullWords === true) {
            $shortened = \mb_substr($text, 0, $charCount, 'UTF-8');
            while (!\in_array(\mb_substr($shortened, -1, 1, 'UTF-8'), static::$wordEndingChars)) {
                $shortened = \mb_substr($shortened, 0, -1, 'UTF-8');
            }
        }
        if ($fullWords !== true) {
            $shortened = \mb_substr($text, 0, $charCount, 'UTF-8');
        }

        while (\in_array(\mb_substr($shortened, -1, 1, 'UTF-8'), static::$wordEndingChars)) {
            $shortened = \mb_substr($shortened, 0, -1, 'UTF-8');
        }

        return $shortened.$suffix;
    }
995770
private function _getActualLength ( $text ) {
        $convertedText = $this->_convertTagsToEscapeSequences($text);
        $strippedText = $this->_stripEscapeSequences($convertedText);
        $length = mb_strlen($strippedText, 'utf-8');
        return $length;
    }
369221
protected function addSuffix($suffix, $string)
    {
        if (substr($string, -strlen($suffix)) !== (string) $suffix) {
            $string .= $suffix;
        }

        return $string;
    }
559563
public function truncate($text, $charactersCount)
    {
        if ($charactersCount < 0 || \mb_strlen($text) <= $charactersCount) {
            return $text;
        }

        $truncatedText = \rtrim(\mb_substr($text, 0, $this->breakpoint->calculatePosition($text, $charactersCount)));

        return ($truncatedText === $text) ? $truncatedText : $truncatedText.$this->append;
    }
623797
public function cropMiddle($text, $maxLength, $delimiter = '...')
    {

        // return text if it doesn't need to be cropped
        if (!$text || strlen($text) <= $maxLength || !substr($text, 0)) {
            return $text;
        }

        $substrLength = floor(($maxLength - strlen($delimiter)) / 2);

        return substr($text, 0, $substrLength).$delimiter.substr($text, -$substrLength);
    }
551321
public function truncate($length = 30, $suffix = '...', $lastSpace = true)
    {
        $string = clone $this;
        $string->htmld();

        if ($string->length() > $length) {
            $string = $string->substr(0, $length - $string->length());

            if (true === $lastSpace) {
                $string->pregReplace('/\s+?(\S+)?$/', '');
            }

            $string->concat($suffix);
        }

        return $string;
    }
532515
private function stringEndsWith(string $string, string $suffix): bool
    {
        return substr($string, -strlen($suffix)) === $suffix;
    }
145671
public function shortenTextWord($textStr, $numChars = 255, $tail = '…')
    {
        if (intval($numChars) == 0) {
            $numChars = 255;
        }
        $textStr = strip_tags($textStr);
        if (function_exists('mb_substr')) {
            if (mb_strlen($textStr, APP_CHARSET) > $numChars) {
                $textStr = preg_replace('/\s+?(\S+)?$/', '', mb_substr($textStr, 0, $numChars + 1, APP_CHARSET));
                // this is needed if the shortened string consists of one single word
                $textStr = mb_substr($textStr, 0, $numChars, APP_CHARSET) . $tail;
            }
        } else {
            if (strlen($textStr) > $numChars) {
                $textStr = preg_replace('/\s+?(\S+)?$/', '', substr($textStr, 0, $numChars + 1));
                $textStr = substr($textStr, 0, $numChars) . $tail;
            }
        }

        return $textStr;
    }
201234
public static function reduce( string $text, string $chars = ' ' ): string {
    $text = preg_replace( '/[' . $chars . ']{2,}/', ' ', $text );
    return $text;
  }
1221542
private function endswith($string, $suffix)
	{
		$strlen = strlen($string);
		$testlen = strlen($suffix);
		if ($testlen > $strlen) {
			return false;
		}

		return substr_compare($string, $suffix, -$testlen) === 0;
	}
711973
public function truncateHtml($str, $length, $suffix = '&hellip;')
    {
        $stringUitl = new StringUtil();

        return $stringUitl->truncatePreservingTags($str, $length, $suffix);
    }
225506
function textLength($t, $mxl, $text) {
    	$taille_c = $t;
    	$len      = mb_strlen($text);
    	while (($taille_c - 2) * $len > $mxl) {
    		$taille_c--;
    		if ($taille_c == 2) {
    			break;
    		}
    	}
    
    	return $taille_c;
    }
175587
public static function truncateWords($str, $length, $trailing='...') {
		$words = explode(' ', $str);
		$cutwords = array_slice($words, 0, $length);

		return implode(' ', $cutwords).(count($words) > count($cutwords) ? $trailing:'');
	}
663099
private void appendText(String text) {

        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(text)) {
            text = Translate.decode(text);
            text = collapse(text);
        }
        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(text)) {

            if (m_storedBrCount > 0) {
                m_appendBr = true;
                appendLinebreak(m_storedBrCount);
            }
            appendIndentation();
            m_brCount = 0;

            List<String> wordList = CmsStringUtil.splitAsList(text, ' ');
            Iterator<String> i = wordList.iterator();
            while (i.hasNext()) {
                String word = i.next();
                boolean hasNbsp = ((word.charAt(0) == 160) || (word.charAt(word.length() - 1) == 160));
                if ((word.length() + 1 + m_lineLength) > m_maxLineLength) {
                    m_appendBr = true;
                    appendLinebreak(1);
                    appendIndentation();
                    m_brCount = 0;
                } else {
                    if (!hasNbsp
                        && (m_lineLength > m_indent)
                        && (m_result.charAt(m_result.length() - 1) != 160)
                        && (m_result.charAt(m_result.length() - 1) != 32)) {

                        m_result.append(' ');
                        m_lineLength++;
                    }
                }
                m_result.append(word);
                m_lineLength += word.length();
            }
        }
    }
598730
protected function truncateHtml (& $text, $maxChars) {
		$texts = [];
		$index = 0;
		$charsCount = 0;
		// explode all html content to array with text contents and html tags
		while (TRUE) {
			$openTagPos = mb_strpos($text, '<', $index);
			if ($openTagPos === FALSE) {
				$subText = mb_substr($text, $index);
				$subText = preg_replace("#\s+#", ' ', str_replace('&nbsp;', ' ', $subText));
				$texts[] = [TRUE, $subText, $charsCount, mb_strlen($subText)];
				break;
			}
			$closeTagPos = mb_strpos($text, '>', $openTagPos + 1);
			if ($closeTagPos === FALSE) {
				$subText = mb_substr($text, $index);
				$subText = preg_replace("#\s+#", ' ', str_replace('&nbsp;', ' ', $subText));
				$texts[] = [TRUE, $subText, $charsCount, mb_strlen($subText)];
				break;
			}
			$subText = mb_substr($text, $index, $openTagPos - $index);
			$subText = preg_replace("#\s+#", ' ', str_replace('&nbsp;', ' ', $subText));
			$subTag = mb_substr($text, $openTagPos, $closeTagPos + 1 - $openTagPos);
			$subTextLength = mb_strlen($subText);
			$subTagLength = mb_strlen($subTag);
			if ($subTextLength > 0)
				$texts[] = [TRUE, $subText, $charsCount, $subTextLength];
			if ($subTagLength > 0)
				$texts[] = [FALSE, $subTag, 0, $subTagLength];
			$charsCount += $subTextLength;
			if ($charsCount >= $maxChars) break;
			$index = $closeTagPos + 1;
		}
		// if there are more chars in text content:
		if ($charsCount > $maxChars) {
			$threeDotsText = $this->threeDotsTexts[1];
			if ($threeDotsText === NULL) $threeDotsText = '&hellip;';
			// try to put three dots from the end into text content where necessary
			for ($i = count($texts) - 1; $i > -1; $i -= 1) {
				list($type, $subText, $charsCount, $subTextLength) = $texts[$i];
				if (!$type) continue;
				$maxCharsLocal = $maxChars - $charsCount;
				$subText = mb_substr($subText, 0, $maxCharsLocal);
				$lastSpacePos = mb_strrpos($subText, ' ');
				if ($lastSpacePos !== FALSE) {
					$subText = rtrim($subText, ',.:;?!+"\'-–()[]{}<>=$§ ');
					if (mb_strlen($subText) === 0) {
						unset($texts[$i]);
					} else {
						$texts[$i][1] = mb_substr($subText, 0, $lastSpacePos) . $threeDotsText;
						break;
					}
				} else {
					unset($texts[$i]);
				}
			}
			// implode truncated text content and all tags (not truncated) back together
			$result = '';
			foreach ($texts as $textRecord) $result .= $textRecord[1];
			return $result;
		} else {
			return $text;
		}
	}
1088494
public static function shortenWordsByChar($input, $num_chars = 35, $hellip = '&nbsp;&hellip;')
    {
        if (strlen($input) <= $num_chars) {
            return $input;
        }

        $shortened = substr($input, 0, $num_chars);
        $shortened_words = array_filter(explode(' ', $shortened));
        end($shortened_words);
        $last_key = key($shortened_words);

        $words = explode(' ', $input);
        $words = array_slice($words, 0, $last_key + 1);
        if ($words[$last_key] !== $shortened_words[$last_key]) {
            unset($words[$last_key]);
        }
        return join(' ', $words) . $hellip;
    }
738773
public function truncate($text, $length = 15)
    {
        if (!$this->isValidLength($text, $length)) return $text;
        return (\strlen($text) > $length) ? $this->left($text, $length) . '...' : $text;
    }