public function truncate($string, $length, $alignment = null, $delimString = null)
    {
        $alignment = $alignment === null ? 'left' : $alignment;
        $delimString = $delimString === null ? '...' : $delimString;
        $delimLen = strlen($delimString);

        if (!in_array($alignment, ['left', 'right'], true)) {
            throw new \InvalidArgumentException(
                'Alignment must either be "left" or "right"'
            );
        }

        if ($delimLen > $length) {
            throw new \InvalidArgumentException(
                sprintf(
                    'Delimiter length "%s" cannot be greater than truncate length "%s"',
                    $delimLen,
                    $length
                )
            );
        }

        if (strlen($string) > $length) {
            $offset = $length - $delimLen;
            if ('left' === $alignment) {
                $string = substr($string, 0, $offset).$delimString;
            } else {
                $string = $delimString.substr($string, strlen($string) - $offset);
            }
        }

        return $string;
    }
public function truncate($string, $length, $alignment = null, $delimString = null)
    {
        $alignment = $alignment === null ? 'left' : $alignment;
        $delimString = $delimString === null ? '...' : $delimString;
        $delimLen = strlen($delimString);

        if (!in_array($alignment, ['left', 'right'], true)) {
            throw new \InvalidArgumentException(
                'Alignment must either be "left" or "right"'
            );
        }

        if ($delimLen > $length) {
            throw new \InvalidArgumentException(
                sprintf(
                    'Delimiter length "%s" cannot be greater than truncate length "%s"',
                    $delimLen,
                    $length
                )
            );
        }

        if (strlen($string) > $length) {
            $offset = $length - $delimLen;
            if ('left' === $alignment) {
                $string = substr($string, 0, $offset).$delimString;
            } else {
                $string = $delimString.substr($string, strlen($string) - $offset);
            }
        }

        return $string;
    }
public function truncate ( $ string , $ length , $ alignment = null , $ delimString = null ) { $ alignment = $ alignment === null ? 'left' : $ alignment ; $ delimString = $ delimString === null ? '...' : $ delimString ; $ delimLen = strlen ( $ delimString ) ; if ( ! in_array ( $ alignment , [ 'left' , 'right' ] , true ) ) { throw new \ InvalidArgumentException ( 'Alignment must either be "left" or "right"' ) ; } if ( $ delimLen > $ length ) { throw new \ InvalidArgumentException ( sprintf ( 'Delimiter length "%s" cannot be greater than truncate length "%s"' , $ delimLen , $ length ) ) ; } if ( strlen ( $ string ) > $ length ) { $ offset = $ length - $ delimLen ; if ( 'left' === $ alignment ) { $ string = substr ( $ string , 0 , $ offset ) . $ delimString ; } else { $ string = $ delimString . substr ( $ string , strlen ( $ string ) - $ offset ) ; } } return $ string ; }
Truncates a string.

@param string $string
@param int    $length
@param string $alignment   - one of "left", "right". default left
@param string $delimString - string to use to mark the truncation

@throws \InvalidArgumentException

@return string
Truncates a string .