UTF-8 safe text truncation.

@param string $text - Text to truncate
@param int $length - Length to truncate to
@param string $suffix - This string will replace the last characters of the text

@return string
187578
public static function truncate($text, $length, $suffix = '...')
    {
        $strlen = mb_strlen($text, 'UTF-8');

        if ($strlen > $length) {
            $truncatedLength = $length - strlen($suffix);
            $text = mb_substr($text, 0, $truncatedLength, 'UTF-8') . $suffix;
        }

        return $text;
    }
1327612
def trunc(text, length):
    
    if length < 1:
        raise ValueError("length should be 1 or larger")

    # Remove whitespace first so no unneccesary truncation is done.
    text = text.strip()
    text_length = wcswidth(text)

    if text_length <= length:
        return text

    # We cannot just remove n characters from the end since we don't know how
    # wide these characters are and how it will affect text length.
    # Use wcwidth to determine how many characters need to be truncated.
    chars_to_truncate = 0
    trunc_length = 0
    for char in reversed(text):
        chars_to_truncate += 1
        trunc_length += wcwidth(char)
        if text_length - trunc_length <= length:
            break

    # Additional char to make room for elipsis
    n = chars_to_truncate + 1
    return text[:-n].strip() + '…'
305380
protected function _text($text, $length, $title = false) {
		$textSanitized = Sanitize::html(preg_replace('/(\[[0-9;]{1,}m)/ims', '', $text));
		if ($title && !$this->_isCli) {
			$textSanitized = $this->Html->tag('span', $textSanitized, array(
				'title' => $text
			));
		}
		return $length ? $this->Text->truncate($textSanitized, $length, array(
					'exact' => true, 'html' => true
				)) : $textSanitized;
	}
200359
public static function limitWords($string, $words = 100, $suffix = '...')
    {
        preg_match('/^\s*+(?:\S++\s*+){1,' . $words . '}/', $string, $matches);

        /** @noinspection UnSafeIsSetOverArrayInspection */
        if (isset($matches[0]) && mb_strlen($matches[0]) < mb_strlen($string)) {
            return trim($matches[0]) . $suffix;
        }

        return $string;
    }
406316
public static function truncate(string $str, $maxLength, $suffix = '...'): string
    {
        if (self::strlen($str) <= $maxLength) {
            return $str;
        }

        $str = \utf8_decode($str);

        return \utf8_encode(\substr($str, 0, $maxLength - self::strlen($suffix)) . $suffix);
    }
1292472
public function trim( $string, $limit = 80, $suffix = '&hellip;' ) {
		if ( $limit < mb_strlen( $string, 'utf-8' ) ) {
			$string = mb_substr( $string, 0, $limit - 1 ) . $suffix;
		}

		return $string;
	}
1101338
public static function endsWith($string, $suffix) {
    return (mb_strrpos($string,$suffix) === mb_strlen($string) - mb_strlen($suffix));
  }
995770
private function _getActualLength ( $text ) {
        $convertedText = $this->_convertTagsToEscapeSequences($text);
        $strippedText = $this->_stripEscapeSequences($convertedText);
        $length = mb_strlen($strippedText, 'utf-8');
        return $length;
    }
761054
function truncate(str, len) {
        // Truncate text to specified length
        if (str.length > len) {
            str = str.substr(0, len);

            // To prevent awkwardly truncating in the middle of a word,
            // attempt to truncate at the end of the last whole word
            var lastSpaceChar = str.lastIndexOf(" ");
            if (lastSpaceChar < len && lastSpaceChar > -1) {
                str = str.substr(0, lastSpaceChar);
            }
            return str;
        }
    }
1267613
public static function subDescription(string $text, int $length): string
    {
        return mb_strlen($text, 'utf-8') > $length ? substr($text, 0, $length - 3) . '...' : $text;
    }
773694
public static function endsWith($subject, $suffix, $encoding = null)
    {
        return Rope::of($subject, $encoding)->endsWith($suffix);
    }
1182013
public static function removeSuffix($string, $suffix)
    {
        if (self::endsWith($string, $suffix)) {
            return mb_substr($string, 0, -mb_strlen($suffix));
        }
        return $string;
    }
104385
static public function suffixToContentType($suffix)
    {
        $types = self::bySuffix();
        return isset($types[$suffix]) ? $types[$suffix]['content-type'] : null;
    }
1221630
public static function truncate($text, $chars = 60) {
        if (!$text)
            return $text;
        
        $lines = explode("\n", $text);
        foreach ($lines as &$line)
            $line = trim($line);
        $text = implode(" ", $lines);
        if (strlen($text) <= $chars)
            return $text;
        
        $text = $text . " ";
        $text = substr($text, 0, $chars);
        $text = substr($text, 0, strrpos($text, ' '));
        $text = $text . "...";
        return $text;
    }
1183530
public static function endsWith($string, $suffix)
    {
        $string = is_int($string) ? "$string" : $string;
        $suffix = is_int($suffix) ? "$suffix" : $suffix;
        $suffixLength = mb_strlen($suffix);
        return is_string($string) && is_string($suffix) && mb_substr($string, -$suffixLength, $suffixLength) === $suffix;
    }
875256
public function truncate($string, $endlength="30", $end="...")
        {
            $strlen = strlen($string);

            if ($strlen > $endlength) {
                $trim = $endlength - $strlen;
                $string = substr($string, 0, $trim);
                $string .= $end;
            }

            return $string;
        }
258366
public function right($text, $length)
    {
        if (!$this->isValidLength($text, $length)) return $text;

        $textLength = strlen($text);

        return \substr($text, $textLength - $length, $textLength);
    }