Latte\Runtime\Filters::truncate PHP Method

truncate() public static method

Truncates string to maximal length.
public static truncate ( $s, $maxLen, $append = "…" ) : string
return string plain text
    public static function truncate($s, $maxLen, $append = "…")
    {
        if (strlen(utf8_decode($s)) > $maxLen) {
            $maxLen = $maxLen - strlen(utf8_decode($append));
            if ($maxLen < 1) {
                return $append;
            } elseif (preg_match('#^.{1,' . $maxLen . '}(?=[\\s\\x00-/:-@\\[-`{-~])#us', $s, $matches)) {
                return $matches[0] . $append;
            } else {
                return self::substring($s, 0, $maxLen) . $append;
            }
        }
        return $s;
    }

Usage Example

Beispiel #1
0
 /** @internal */
 protected function checkExtraArgs(MacroNode $node)
 {
     if ($node->tokenizer->isNext()) {
         $args = Latte\Runtime\Filters::truncate($node->tokenizer->joinAll(), 20);
         trigger_error("Unexpected arguments '{$args}' in " . $node->getNotation());
     }
 }