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 : '');
}