public function truncate($length, $substring = '')
    {
        if ($length >= $this->getLength()) return $this;

        // Need to further trim the string so we can append the substring
        $substringLength = UTF8::strlen($substring, $this->encoding);
        $length -= $substringLength;

        $truncated = UTF8::substr
        (
            $this->scalarString,
            0,
            $length,
            $this->encoding
        );

        return $this->newSelf($truncated . $substring);
    }
public function truncate($length, $substring = '')
    {
        if ($length >= $this->getLength()) return $this;

        // Need to further trim the string so we can append the substring
        $substringLength = UTF8::strlen($substring, $this->encoding);
        $length -= $substringLength;

        $truncated = UTF8::substr
        (
            $this->scalarString,
            0,
            $length,
            $this->encoding
        );

        return $this->newSelf($truncated . $substring);
    }
public function truncate ( $ length , $ substring = '' ) { if ( $ length >= $ this -> getLength ( ) ) return $ this ; // Need to further trim the string so we can append the substring $ substringLength = UTF8 :: strlen ( $ substring , $ this -> encoding ) ; $ length -= $ substringLength ; $ truncated = UTF8 :: substr ( $ this -> scalarString , 0 , $ length , $ this -> encoding ) ; return $ this -> newSelf ( $ truncated . $ substring ) ; }
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.

@param  int    $length    Desired length of the truncated string.

@param  string $substring The substring to append if it can fit.

@return static            String after truncating.
Truncates the string to a given length .