Truncate text after x chars (and add suffix, like '...')

@param  int    $length
@param  string $suffix
@param  bool   $lastSpace
@return Strings  Truncated string.
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;
    }
1072553
private function generateCode(int $length = 4, string $prefix = '', string $suffix = '')
    {
        for ($i = 0; $i < $length; $i++) {
            $prefix .= random_int(0, 1) ? chr(random_int(65, 90)) : random_int(0, 9);
        }

        return $prefix.$suffix;
    }
978963
function truncate(input, length, suffix, preserve) {

  length = isUndefined(length) ? input.length : length;
  preserve = preserve || false;
  suffix = suffix || '';

  if(!isString(input) || (input.length <= length)) return input;

  return input.substring(0, (preserve) ?
    ((input.indexOf(' ', length) === -1) ? input.length : input.indexOf(' ', length)) :
    length) + suffix;
}
924361
public static function truncateValue($string, $limit, $endchars = '...')
    {

        if (!is_array($string) && !is_object($string)) {
            $stringLength = strlen($string);
            $endcharsLength = strlen($endchars);

            if ($stringLength > (int) $limit) {
                $cut = (int) ($limit - $endcharsLength);
                $string = substr($string, 0, $cut) . $endchars;
            }
        }
        return $string;
    }
442202
protected function truncateText (& $text, $maxChars) {
		if (mb_strlen($text) > $maxChars) {
			$text = preg_replace("#\s+#", ' ', $text);
			$text = mb_substr($text, 0, $maxChars);
			$lastSpacePos = mb_strrpos($text, ' ');
			if ($lastSpacePos !== FALSE) {
				$threeDotsText = $this->threeDotsTexts[0];
				if ($threeDotsText === NULL) $threeDotsText = '...';
				$text = rtrim($text, ',.:;?!+"\'-–()[]{}<>=$§ ');
				$text = mb_substr($text, 0, $lastSpacePos) . $threeDotsText;
			}
		}
	}
958150
public function appendSpace($length)
    {
        $paddingLength = (int) $length;
        $this->message = $this->message . str_pad('', $paddingLength, ' ');

        return $this;
    }
640592
public function shorten($str, $length = 100, $end = '&#8230;')
    {

        if (strlen($str) > $length)
        {
            $str = \substr(trim($str), 0, $length);
            $str = \substr($str, 0, -\strpos(\strrev($str), ' '));
            $str = \trim($str . $end);
        }

        return $str;
    }
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;
    }
1101338
public static function endsWith($string, $suffix) {
    return (mb_strrpos($string,$suffix) === mb_strlen($string) - mb_strlen($suffix));
  }
591297
private function getExcerpt($text, $length = 250, $separator = ' &hellip;')
    {
        // use regex instead of strip_tags to left some spaces when removing tags
        $text = preg_replace('#<[^>]+>#', ' ', $text);

        // trim whitespace at beginning or end of string
        // See: http://stackoverflow.com/a/4167053/569101
        $text = preg_replace('/^[\pZ\pC]+|[\pZ\pC]+$/u', '', $text);
        // clean new lines and tabs
        $text = trim(preg_replace("/[\n\r\t ]+/", ' ', $text), ' ');

        if (mb_strlen($text) > $length) {
            // If breakpoint is on the last word, return the text without separator.
            if (false === ($breakpoint = mb_strpos($text, ' ', $length))) {
                return $text;
            }
            $length = $breakpoint;

            return rtrim(mb_substr($text, 0, $length)) . $separator;
        }

        return $text;
    }
1308923
public static function snippet(string $text, int $length = 150)
    {
        $breakerPos = mb_strpos($text, self::WYSIWYG_BREAK_HTML, null, 'UTF-8');
        // offset is founded, try to split preview from full text
        if ($breakerPos !== false) {
            $text = Str::sub($text, 0, $breakerPos);
        } else { // page breaker is not founded, lets get a fun ;D
            // find first paragraph ending
            $breakerPos = mb_strpos($text, '</p>', null, 'UTF-8');
            // no paragraph's ? lets try to get <br[\/|*]>
            if ($breakerPos === false) {
                $breakerPos = mb_strpos($text, '<br', null, 'UTF-8');
            } else {
                // add length('</p>')
                $breakerPos+= 4;
            }
            // cut text from position caret before </p> (+4 symbols to save item as valid)
            if ($breakerPos !== false) {
                $text = Str::sub($text, 0, $breakerPos);
            }
        }

        // if breaker position is still undefined - lets make 'text cut' for defined length and remove all html tags
        if ($breakerPos === false) {
            $text = strip_tags($text);
            $text = self::cut($text, 0, $length);
        }

        return $text;
    }
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;
        }
690451
static function End($string, $length = 0)
    {    
        self::InitEncoding();
        $pos = max(0, self::Length($string) - $length);
        return self::Part($string, $pos);
    }
816615
public function truncate($message, $length, $suffix = '...')
    {
        $computedLength = $length - $this->strlen($suffix);

        if ($computedLength > $this->strlen($message)) {
            return $message;
        }

        if (false === $encoding = mb_detect_encoding($message, null, true)) {
            return substr($message, 0, $length).$suffix;
        }

        return mb_substr($message, 0, $length, $encoding).$suffix;
    }
831924
public function endsWith($suffix)
    {
        return $suffix === ''
        || $suffix === mb_substr(
            $this->contents,
            -strlen((string) $suffix),
            null,
            $this->encoding
        );
    }
829465
public function truncate(int $length = 100, string $append = '...') : Manipulator
    {
        return new static(mb_substr($this->string, 0, $length) . $append);
    }
1001987
public static function cutStr($str, $length=100, $postfix='...')
    {
        if ( strlen($str) < $length)
            return $str;

        $temp = substr($str, 0, $length);
        return substr($temp, 0, strrpos($temp, ' ') ) . $postfix;
    }