Returns part of string preserving words

@param string $text      The string / text
@param int    $maxLength Maximum length of given string
@param string $suffix    (optional) The suffix to add at the end of string
@return string
330202
public static function substringToWord($text, $maxLength, $suffix = '...')
    {
        $effect = $text;

        $textLength = mb_strlen($text, 'utf-8');
        $suffixLength = mb_strlen($suffix, 'utf-8');

        $maxLength -= $suffixLength;

        if ($textLength > $maxLength) {
            $effect = mb_substr($text, 0, $maxLength, 'utf-8');
            $lastSpacePosition = mb_strrpos($effect, ' ', 'utf-8');

            if (false !== $lastSpacePosition) {
                $effect = mb_substr($effect, 0, $lastSpacePosition, 'utf-8');
            }

            $effect .= $suffix;
        }

        return $effect;
    }
1218454
private static function makeLine(string $text, ?string $comment, int $longestVarLength, int $padding): string
    {
        $output = str_repeat(' ', $padding).str_pad($text, $longestVarLength);

        if (null !== $comment) {
            $output .= ' '.self::COMMENT_DELIMINATOR.$comment;
        }

        return $output.PHP_EOL;
    }
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);
    }
1270482
public static function truncate(string $string, int $maxLength, string $indicator = '...'): string
    {
        if (mb_strlen($string) > $maxLength) {
            $string = mb_substr($string, 0, $maxLength - mb_strlen($indicator)) . $indicator;
        }

        return $string;
    }
1278009
public static function shorten($text, $maxlength = 140, $end = '[...]') {
        $maxlength++;
        if (mb_strlen($text) > $maxlength) {
            $subex = mb_substr($text, 0, $maxlength - 5);
            $exwords = explode(' ', $subex);
            $excut = - (mb_strlen($exwords[count($exwords)-1]));
            if ($excut < 0) {
                $text = mb_substr($subex, 0, $excut);
            } else {
                $text = $subex;
            }
            $text .= $end;
        }
        return $text;
    }
1028685
public static function abbreviate($string, $maxWidth)
    {
        if (mb_strlen($string) > $maxWidth) {
            return mb_substr($string, 0, $maxWidth) . '...';
        }
        return $string;
    }
1191507
public function setMaxLength($maxLength)
    {
        if ($this->type !== self::TYPE_STRING) {
            throw new \Exception('maxLength can only be set on type "string"');
        }

        $this->maxLength = (int) $maxLength;
    }
253015
public function get_short_filename($str, $maxlength) {
        if (core_text::strlen($str) >= $maxlength) {
            return trim(core_text::substr($str, 0, $maxlength)).'...';
        } else {
            return $str;
        }
    }
1347348
public static function truncateHTML($html, $maxLength, $trailing='...') {
		$html = trim($html);
		$printedLength = 0;
		$position = 0;
		$tags = [];

		$res = '';

		while ($printedLength < $maxLength && preg_match('{</?([a-z]+)[^>]*>|&#?[a-zA-Z0-9]+;}', $html, $match, PREG_OFFSET_CAPTURE, $position)) {
			list($tag, $tagPosition) = $match[0];

			$str = substr($html, $position, $tagPosition - $position);
			if($printedLength + strlen($str) > $maxLength) {
				$res .= (substr($str, 0, $maxLength - $printedLength));
				$printedLength = $maxLength;
				break;
			}

			$res .= ($str);
			$printedLength += strlen($str);

			if($tag[0] == '&') {
				$res .= ($tag);
				$printedLength++;
			}
			else {
				$tagName = $match[1][0];
				if($tag[1] == '/')
					$res .= ($tag);
				elseif($tag[strlen($tag) - 2] == '/' || $tagName == 'br' || $tagName == 'hr')
					$res .= ($tag);
				else {
					$res .= ($tag);
					$tags[] = $tagName;
				}
			}

			$position = $tagPosition + strlen($tag);
		}

		if($printedLength < $maxLength && $position < strlen($html))
			$res .= (substr($html, $position, $maxLength - $printedLength));

		if($position < strlen($html))
			$res .= $trailing;

		while(!empty($tags))
			$res .= sprintf('</%s>', array_pop($tags));

		return $res;
	}
722957
protected function limitedText($text, $limit = 200)
    {
        $postString = '...';
        $strCount = strlen(trim($text));
        $sLimitedText = $text;

        if($strCount > $limit)
        {
            $sLimitedText = substr($text, 0, $limit) . $postString;
        }

        return $sLimitedText;

    }
209010
static function snippet($text, $keywords, $limit, $max_length, $around = array('<b>', '</b>'))
	{
		$result = array();

		$keywords = self::normalize($keywords);
		list($pre, $post) = $around;
		$prev = array();
		$after = false;

		foreach(self::normalize_hash($text) as $word_orig => $word_norm)
		{
			$is_kw = false;
			if(in_array($word_norm, $keywords))
			{
				$prev[] = $pre.$word_orig.$post;
				$is_kw = true;
			}
			else
				$prev[] = $word_orig;

			if($is_kw)
			{
				if($after)
				{
					$result = array_merge($result, $prev);
					$prev = array();
				}
				else
				{
					$result = array_merge($result, count($prev) > $limit ? array('&#133') : array(), array_slice($prev, -$limit-1));
					$prev = array();
				}

				$after = true;
			}
			else
			{
				if($after && count($prev) >= $limit)
				{
					$after = false;
					$result = array_merge($result, $prev);
					$prev = array();
				}
			}
		}

		if($after)
			$result = array_merge($result, array('&#133'), $prev);

		return strip_text(join(' ', $result).'&#133;', $max_length);
	}
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;
			}
		}
	}
1233149
public function getMessageLines($text)
    {
        $message = $text;
        $wrapLength = 40;
        // wrap the message to max chars
        $message = wordwrap($message, $wrapLength - 2);
        // split into array of lines
        return explode("\n", $message);
    }
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;
    }
434033
public function getStringValue($key, $maxlength = null)
    {
        $value = $this->get($key);
        if ($value === null) {
            return null;
        }
        return ($maxlength === null) ? $value : substr($value, 0, $maxlength);
    }
951837
public function getContents($maxLength = NULL) {

    if (isset($maxLength)) {
      $ret = @file_get_contents(
        (string) $this,
        NULL, // flags
        NULL, // context
        NULL, // offset
        $maxLength
      );
    } else {
      $ret = @file_get_contents((string) $this);
    }

    if ($ret === FALSE) {
      $reason = '';
      if (!$this->exists()) {
        $reason = '. The file does not exist';
      } elseif(!$this->isReadable()) {
        $reason = '. The file is not readable';
      }

      throw new Exception(
        sprintf("dateiinhalt von '%s' konnte nicht ausgelesen werden PHP::file_get_contents gab FALSE zurück%s", $this, $reason)
      );
    }

    return $ret;
  }
302995
function _trimText(text, maxLength) {
  if(text) {
    text = text.toString();
    if(text.length > maxLength) {
      text = text.substr(0, maxLength - 1) + '..';
    }
  }
  return text;
}