Stringy\Stringy::truncate PHP Метод

truncate() публичный метод

Truncates the string to a given length. If $substring is provided, and truncating occurs, the string is further truncated so that the substring may be appended without exceeding the desired length.
public truncate ( integer $length, string $substring = '' ) : Stringy
$length integer Desired length of the truncated string
$substring string The substring to append if it can fit
Результат Stringy Object with the resulting $str after truncating
    public function truncate($length, $substring = '')
    {
        $stringy = static::create($this->str, $this->encoding);
        if ($length >= $stringy->length()) {
            return $stringy;
        }
        // Need to further trim the string so we can append the substring
        $substringLength = \mb_strlen($substring, $stringy->encoding);
        $length = $length - $substringLength;
        $truncated = \mb_substr($stringy->str, 0, $length, $stringy->encoding);
        $stringy->str = $truncated . $substring;
        return $stringy;
    }