core-bundle/src/Resources/contao/library/Contao/StringUtil.php
public static function substr($strString, $intNumberOfChars, $strEllipsis=' …')
	{
		$strString = preg_replace('/[\t\n\r]+/', ' ', $strString);
		$strString = strip_tags($strString);

		if (Utf8::strlen($strString) <= $intNumberOfChars)
		{
			return $strString;
		}

		$intCharCount = 0;
		$arrWords = array();
		$arrChunks = preg_split('/\s+/', $strString);
		$blnAddEllipsis = false;

		foreach ($arrChunks as $strChunk)
		{
			$intCharCount += Utf8::strlen(static::decodeEntities($strChunk));

			if ($intCharCount++ <= $intNumberOfChars)
			{
				$arrWords[] = $strChunk;
				continue;
			}

			// If the first word is longer than $intNumberOfChars already, shorten it
			// with Utf8::substr() so the method does not return an empty string.
			if (empty($arrWords))
			{
				$arrWords[] = Utf8::substr($strChunk, 0, $intNumberOfChars);
			}

			if ($strEllipsis !== false)
			{
				$blnAddEllipsis = true;
			}

			break;
		}

		// Deprecated since Contao 4.0, to be removed in Contao 5.0
		if ($strEllipsis === true)
		{
			@trigger_error('Passing "true" as third argument to StringUtil::substr() has been deprecated and will no longer work in Contao 5.0. Pass the ellipsis string instead.', E_USER_DEPRECATED);

			$strEllipsis = ' …';
		}

		return implode(' ', $arrWords) . ($blnAddEllipsis ? $strEllipsis : '');
	}
public static function substr($strString, $intNumberOfChars, $strEllipsis=' …')
	{
		$strString = preg_replace('/[\t\n\r]+/', ' ', $strString);
		$strString = strip_tags($strString);

		if (Utf8::strlen($strString) <= $intNumberOfChars)
		{
			return $strString;
		}

		$intCharCount = 0;
		$arrWords = array();
		$arrChunks = preg_split('/\s+/', $strString);
		$blnAddEllipsis = false;

		foreach ($arrChunks as $strChunk)
		{
			$intCharCount += Utf8::strlen(static::decodeEntities($strChunk));

			if ($intCharCount++ <= $intNumberOfChars)
			{
				$arrWords[] = $strChunk;
				continue;
			}

			// If the first word is longer than $intNumberOfChars already, shorten it
			// with Utf8::substr() so the method does not return an empty string.
			if (empty($arrWords))
			{
				$arrWords[] = Utf8::substr($strChunk, 0, $intNumberOfChars);
			}

			if ($strEllipsis !== false)
			{
				$blnAddEllipsis = true;
			}

			break;
		}

		// Deprecated since Contao 4.0, to be removed in Contao 5.0
		if ($strEllipsis === true)
		{
			@trigger_error('Passing "true" as third argument to StringUtil::substr() has been deprecated and will no longer work in Contao 5.0. Pass the ellipsis string instead.', E_USER_DEPRECATED);

			$strEllipsis = ' …';
		}

		return implode(' ', $arrWords) . ($blnAddEllipsis ? $strEllipsis : '');
	}
public static function substr ( $ strString , $ intNumberOfChars , $ strEllipsis = ' …') { $ strString = preg_replace ( '/[\t\n\r]+/' , ' ' , $ strString ) ; $ strString = strip_tags ( $ strString ) ; if ( Utf8 :: strlen ( $ strString ) <= $ intNumberOfChars ) { return $ strString ; } $ intCharCount = 0 ; $ arrWords = array ( ) ; $ arrChunks = preg_split ( '/\s+/' , $ strString ) ; $ blnAddEllipsis = false ; foreach ( $ arrChunks as $ strChunk ) { $ intCharCount += Utf8 :: strlen ( static :: decodeEntities ( $ strChunk ) ) ; if ( $ intCharCount ++ <= $ intNumberOfChars ) { $ arrWords [ ] = $ strChunk ; continue ; } // If the first word is longer than $intNumberOfChars already, shorten it // with Utf8::substr() so the method does not return an empty string. if ( empty ( $ arrWords ) ) { $ arrWords [ ] = Utf8 :: substr ( $ strChunk , 0 , $ intNumberOfChars ) ; } if ( $ strEllipsis !== false ) { $ blnAddEllipsis = true ; } break ; } // Deprecated since Contao 4.0, to be removed in Contao 5.0 if ( $ strEllipsis === true ) { @ trigger_error ( 'Passing "true" as third argument to StringUtil::substr() has been deprecated and will no longer work in Contao 5.0. Pass the ellipsis string instead.' , E_USER_DEPRECATED ) ; $ strEllipsis = ' …'; } return implode ( ' ' , $ arrWords ) . ( $ blnAddEllipsis ? $ strEllipsis : '' ) ; }
Shorten a string to a given number of characters

The function preserves words, so the result might be a bit shorter or
longer than the number of characters given. It strips all tags.

@param string  $strString        The string to shorten
@param integer $intNumberOfChars The target number of characters
@param string  $strEllipsis      An optional ellipsis to append to the shortened string

@return string The shortened string
Shorten a string to a given number of characters