Contao\StringUtil::substr PHP Метод

substr() публичный статический Метод

The function preserves words, so the result might be a bit shorter or longer than the number of characters given. It strips all tags.
public static substr ( string $strString, integer $intNumberOfChars, string $strEllipsis = ' …' ) : string
$strString string The string to shorten
$intNumberOfChars integer The target number of characters
$strEllipsis string An optional ellipsis to append to the shortened string
Результат string The shortened string
    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 : '');
    }

Usage Example

 /**
  * 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
  */
 public static function substr($strString, $intNumberOfChars, $strEllipsis = ' …')
 {
     if (self::isStringUtilAvailable()) {
         return StringUtil::substr($strString, $intNumberOfChars, $strEllipsis);
     }
     return \Contao\String::substr($strString, $intNumberOfChars, $strEllipsis);
 }
All Usage Examples Of Contao\StringUtil::substr