Wraps a string to a given number of characters.

@param  string $string String
@param  int    $width  Max line width
@return string
15660
protected function wordWrap(string $string, int $width): string
	{
		return trim(preg_replace(sprintf('/(.{1,%1$u})(?:\s|$)|(.{%1$u})/uS', $width), '$1$2' . PHP_EOL, $string));
	}
867862
public function getWrappedText($size, $angle, $fontface, $string, $width = 100)
    {
        $str = '';
        $words = explode(' ', $string);

        foreach ($words as $word) {
            $testStr = $str . ' ' . $word;
            $box = imagettfbbox($size, $angle, $fontface, $testStr);

            if ($box[2] > $width) {
                $str .= ($str == '' ? '' : "\n") . $word;
            } else {
                $str .= ($str == '' ? '' : ' ') . $word;
            }
        }

        $this->string = $str;
        $lines = explode("\n", $str);

        return $lines;
    }
1286526
private static function multilineQuote($string)
    {
        $lines = \explode("\n", $string);
        $last = \count($lines) - 1;

        foreach ($lines as $k => $line) {
            if ($k === $last) {
                $lines[$k] = self::convertString($line);
            } else {
                $lines[$k] = self::convertString($line . "\n");
            }
        }

        return $lines;
    }
767735
protected function escape(string $string): string
	{
		if($this->formatter !== null)
		{
			return $this->formatter->escape($string);
		}

		return $string;
	}
1040763
private function _charpad($string, $length, $direction, $filler = ' ')
    {

        // Keep length of string handy here
        $strlen = strlen($string);

        // Difference between max length and string length
        $difference = $length - $strlen;

        // If the string length is bigger than the max, just return string truncated to the max length
        if ($difference < 0) {
            return substr($string, 0, $length);
        }

        // Deal with direction
        switch ($direction) {

            // Right aligned (padded to the left)
            case 'r':
                return str_repeat($filler, $difference - 2).$string;
            break;

            // Left aligned (padded to the right)
            case 'l':
                return $string.str_repeat($filler, $difference - 2);
            break;

            // Centered (padded left and right)
            case 'c':
                $cdiff = floor($difference / 2) - ($difference % 2 == 0 ? 1 : 0);

                return str_repeat($filler, $cdiff - 1).$string.str_repeat($filler, $cdiff);
            break;

            // Not padded; returned as is (provided not longer than max, as tested above)
            case 'n':
                return $string;
            break;

            // Uhh not sure?
            default:
                return '';
            break;
        }
    }
842607
protected function strlen($string)
    {
        // don't count color codes
        $string = preg_replace("/\33\\[\\d+(;\\d+)?m/", '', $string);

        if (function_exists('mb_strlen')) {
            return mb_strlen($string, 'utf-8');
        }

        return strlen($string);
    }
1231460
private function stringToNum(string $string) : BigNumber
    {
        $number = new BigNumber(0);
        foreach (str_split(strrev($string)) as $char) {
            $number->multiply($this->alphabetLength)->add(array_search($char, $this->alphabet, false));
        }

        return $number;
    }
859158
public function encodeString($string, $firstLineOffset = 0, $maxLineLength = 0)
    {
        return str_replace([' ', '=20', "=\r\n"], ['_', '_', "\r\n"],
            parent::encodeString($string, $firstLineOffset, $maxLineLength)
        );
    }
233623
public static String truncate(String string, int maxBytesLength) {
        byte[] stringBytes = string.getBytes();
        return stringBytes.length > maxBytesLength
                ? new String(stringBytes, 0, maxBytesLength - 1) : string;
    }
1329623
public static function wordWrapEnable($str, $width)
    {
        if (!empty($str) && !empty($width) && $width >= 1) {
            return wordwrap($params['str'], $params['width'], '<br />\n');
        } else {
            return false;
        }
    }
1011755
public function encodeString(string $string, ?string $charset = 'utf-8', int $firstLineOffset = 0, int $maxLineLength = 0): string
    {
        if ($maxLineLength > 76 || $maxLineLength <= 0) {
            $maxLineLength = 76;
        }

        $thisLineLength = $maxLineLength - $firstLineOffset;

        $lines = [];
        $lNo = 0;
        $lines[$lNo] = '';
        $currentLine = &$lines[$lNo++];
        $size = $lineLen = 0;
        $charStream = new CharacterStream($string, $charset);

        // Fetching more than 4 chars at one is slower, as is fetching fewer bytes
        // Conveniently 4 chars is the UTF-8 safe number since UTF-8 has up to 6
        // bytes per char and (6 * 4 * 3 = 72 chars per line) * =NN is 3 bytes
        while (null !== $bytes = $charStream->readBytes(4)) {
            $enc = $this->encodeByteSequence($bytes, $size);

            $i = strpos($enc, '=0D=0A');
            $newLineLength = $lineLen + (false === $i ? $size : $i);

            if ($currentLine && $newLineLength >= $thisLineLength) {
                $lines[$lNo] = '';
                $currentLine = &$lines[$lNo++];
                $thisLineLength = $maxLineLength;
                $lineLen = 0;
            }

            $currentLine .= $enc;

            if (false === $i) {
                $lineLen += $size;
            } else {
                // 6 is the length of '=0D=0A'.
                $lineLen = $size - strrpos($enc, '=0D=0A') - 6;
            }
        }

        return $this->standardize(implode("=\r\n", $lines));
    }
808686
public static function cut(string $string, int $limit = 25, string $append = '...'): string
    {
        if (mb_strlen($string, 'UTF-8') <= $limit) {
            return $string;
        }

        return rtrim(mb_substr($string, 0, $limit, 'UTF-8')) . $append;
    }
802
protected function _strlen($string)
    {
        $encoding = function_exists('mb_detect_encoding') ? mb_detect_encoding($string) : false;
        return $encoding ? mb_strlen($string, $encoding) : strlen($string);
    }
1028685
public static function abbreviate($string, $maxWidth)
    {
        if (mb_strlen($string) > $maxWidth) {
            return mb_substr($string, 0, $maxWidth) . '...';
        }
        return $string;
    }
717886
public function wordWrap($string, $width = 75, $break = "\n", $cut = false)
    {
        $string = (string) $string;
        if ($string === '') {
            return '';
        }

        $break = (string) $break;
        if ($break === '') {
            throw new Exception\InvalidArgumentException('Break string cannot be empty');
        }

        $width = (int) $width;
        if ($width === 0 && $cut) {
            throw new Exception\InvalidArgumentException('Cannot force cut when width is zero');
        }

        if (StringUtils::isSingleByteEncoding($this->getEncoding())) {
            return wordwrap($string, $width, $break, $cut);
        }

        $stringWidth = $this->strlen($string);
        $breakWidth  = $this->strlen($break);

        $result    = '';
        $lastStart = $lastSpace = 0;

        for ($current = 0; $current < $stringWidth; $current++) {
            $char = $this->substr($string, $current, 1);

            $possibleBreak = $char;
            if ($breakWidth !== 1) {
                $possibleBreak = $this->substr($string, $current, $breakWidth);
            }

            if ($possibleBreak === $break) {
                $result    .= $this->substr($string, $lastStart, $current - $lastStart + $breakWidth);
                $current   += $breakWidth - 1;
                $lastStart  = $lastSpace = $current + 1;
                continue;
            }

            if ($char === ' ') {
                if ($current - $lastStart >= $width) {
                    $result    .= $this->substr($string, $lastStart, $current - $lastStart) . $break;
                    $lastStart  = $current + 1;
                }

                $lastSpace = $current;
                continue;
            }

            if ($current - $lastStart >= $width && $cut && $lastStart >= $lastSpace) {
                $result    .= $this->substr($string, $lastStart, $current - $lastStart) . $break;
                $lastStart  = $lastSpace = $current;
                continue;
            }

            if ($current - $lastStart >= $width && $lastStart < $lastSpace) {
                $result    .= $this->substr($string, $lastStart, $lastSpace - $lastStart) . $break;
                $lastStart  = $lastSpace = $lastSpace + 1;
                continue;
            }
        }

        if ($lastStart !== $current) {
            $result .= $this->substr($string, $lastStart, $current - $lastStart);
        }

        return $result;
    }
1159787
protected static function unfoldString( $string )
	{
		$string	= str_replace( self::$lineBreak." ;", ";", $string );
		$string	= str_replace( self::$lineBreak." :", ":", $string );
		$string	= str_replace( self::$lineBreak." ", "", $string );
		return $string;
	}
135611
public function wordWrap($string)
    {
        return wordwrap($string, $this->line_length, $this->break_string, $this->split_mid_word);
    }