Limits the number of characters in a string.

@param  string $string     The input string
@param  int    $characters Number of characters to allow
@param  string $sufix      Sufix to add if number of characters is reduced
@return string
164201
public static function limitChars(string $string, int $characters = 100, string $sufix = '...'): string
	{
		return (mb_strlen($string) > $characters) ? trim(mb_substr($string, 0, $characters)) . $sufix : $string;
	}
1235430
static function Trim($string, $chars = null)
    {
        self::InitEncoding();
        if (!$chars)
            return trim($string);

        return trim($string, $chars);
    }
1323228
public static function isLength($string = false, $minLength = 0, $maxLength = false) {

        if ($string && strlen($string) >= $minLength) {

            if (!$maxLength || strlen($string) <= $maxLength) {

                return true;
            }
        }

        return false;
    }
900128
public function char($column, $length = null)
    {
        $length = $length ?: Builder::$defaultStringLength;

        return $this->addColumn('char', $column, compact('length'));
    }
363983
protected function truncate($string, $limit, $pad = '...', $break = ' ')
    {
        // return with no change if string is shorter than $limit
        if(strlen($string) <= $limit) return $string;

        // is $break present between $limit and the end of the string?
        if(false !== ($breakpoint = strpos($string, $break, $limit))) {
            if($breakpoint < strlen($string) - 1) {
                $string = substr($string, 0, $breakpoint) . $pad;
            }
        }

        return $string;
    }
624301
public static function truncate($text, $charactersCount, $append = '')
    {
        $truncate = new TextTruncate(new WordBreakpoint(), $append);

        return $truncate->truncate($text, $charactersCount);
    }
11345
public function encodeString(string $string, ?string $charset = 'utf-8', int $firstLineOffset = 0, int $maxLineLength = 0): string
    {
        $lines = [];
        $lineCount = 0;
        $lines[] = '';
        $currentLine = &$lines[$lineCount++];

        if (0 >= $maxLineLength) {
            $maxLineLength = 75;
        }

        $charStream = new CharacterStream($string, $charset);
        $thisLineLength = $maxLineLength - $firstLineOffset;

        while (null !== $char = $charStream->read(4)) {
            $encodedChar = rawurlencode($char);
            if (0 !== \strlen($currentLine) && \strlen($currentLine.$encodedChar) > $thisLineLength) {
                $lines[] = '';
                $currentLine = &$lines[$lineCount++];
                $thisLineLength = $maxLineLength;
            }
            $currentLine .= $encodedChar;
        }

        return implode("\r\n", $lines);
    }
479342
public function getCharIndex(string $char): int
    {
        $index = strpos($this->charsSet, $char);
        if ($index === false) {
            throw DictionaryException::charNotFound();
        }

        return $index;
    }
774010
public static function codePointAt($chars, int $index, int $limit = null): int
    {
        if (\is_array($chars) === false && ($chars instanceof CharSequence) === false) {
            throw new InvalidArgumentException('Only char arrays and sequences can be processed!');
        }

        // Validate char array
        if (\is_array($chars)) {
            $test = reset($chars);

            if ($test === false || ($test instanceof static) === false) {
                throw new InvalidArgumentException('Array must contain char elements!');
            }
        }

        // Validate specified index
        if ($index < 0) {
            throw new InvalidArgumentException('Index cannot be negative!');
        }

        // Validate length
        if ($limit !== null) {
            if ($limit < 0 || $index >= $limit) {
                throw new InvalidArgumentException('Limit cannot be negative and index must be lower than specified limit!');
            }

            $length = \is_array($chars) ? count($chars) : $chars->length();

            if ($limit > $length) {
                throw new InvalidArgumentException('Limit cannot be greater than char array/sequence!');
            }
        }

        $char       = \is_array($chars) ? $chars[$index] : $chars->charAt($index);
        $stringChar = (string) $char;

        return mb_ord($stringChar, mb_detect_encoding($stringChar));
    }
1069751
public static function random($length = 64, $characters = self::ALNUM)
    {
        static $random_generator = null;

        if (null === $random_generator) {
            $random_generator = is_callable('mt_rand') ? 'mt_rand' : 'rand';
        }

        $len = strlen($characters);
        return join(
            '',
            array_map(
                function () use ($characters, $len, $random_generator) {
                    return $characters{$random_generator(0, $len - 1)};
                },
                range(0, $length - 1)
            )
        );
    }
607130
protected function limitConsecutiveNewlines($string, $limit = 2)
    {
        // Pattern that finds any consecutive newlines that exceed the allowed limit.
        $exceeded = $limit + 1;
        $pattern = '/((\\n){' . $exceeded . ',})/';

        // Create a string with the maximum number of allowed newlines.
        $newlines = '';
        for ($i = 0; $i < $limit; $i++) {
            $newlines .= PHP_EOL;
        }

        // Find each match and replace it with the maximum number of newlines.
        return preg_replace($pattern, $newlines, $string);
    }
76840
private static function make($characters, $length)
    {
        $token = '';

        do {
            $token .= $characters[random_int(0, count($characters) - 1)];
        } while (strlen($token) < $length);

        return $token;
    }
566185
public static function len(string $string): int
    {
        if (false === $encoding = \mb_detect_encoding($string, null, true)) {
            return \strlen($string);
        }

        return \mb_strwidth($string, $encoding);
    }
150014
public static function mustBeginWith($character = '/', $string = ''){
        if (strlen($string) > 0) {
            if (substr($string, 0, 1) != $character) {
                return $character.$string;
            } else {
                return $string;
            }
        } else {
            return $character;
        }
    }
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;
	}
935414
public static function limitWords($string, $limit = 100, $append = '...'): string
    {
        preg_match('/^\s*+(?:\S++\s*+){1,' . $limit . '}/u', $string, $matches);

        if (!Arr::key(0, $matches) || self::len($string) === self::len($matches[0])) {
            return $string;
        }

        return rtrim($matches[0]) . $append;
    }
900118
public static function excerpt ($string, $characters, $ellipsis = '...')
		{
			$string = strip_tags($string);

			if (strlen($string) > $characters) {
				return substr($string, 0, $characters) . $ellipsis;
			} else {
				return $string;
			}
		}