public static function summarize(
        string $str,
        int $maxLength,
        string $ellipsis = "...",
        string $needle = ""
    ): string
    {
        $length = mb_strlen($str);
        if ($length <= $maxLength) {
            return $str;
        }

        $endPos = $maxPos = $maxLength - mb_strlen($ellipsis);
        if ($needle !== "") {
            $offset = $maxPos - $length;
            $endPos = mb_strrpos($str, $needle, $offset);
            if ($endPos === false) {
                $endPos = $maxPos;
            }
        }

        return mb_substr($str, 0, $endPos).$ellipsis;
    }
public static function summarize(
        string $str,
        int $maxLength,
        string $ellipsis = "...",
        string $needle = ""
    ): string
    {
        $length = mb_strlen($str);
        if ($length <= $maxLength) {
            return $str;
        }

        $endPos = $maxPos = $maxLength - mb_strlen($ellipsis);
        if ($needle !== "") {
            $offset = $maxPos - $length;
            $endPos = mb_strrpos($str, $needle, $offset);
            if ($endPos === false) {
                $endPos = $maxPos;
            }
        }

        return mb_substr($str, 0, $endPos).$ellipsis;
    }
public static function summarize ( string $ str , int $ maxLength , string $ ellipsis = "..." , string $ needle = "" ) : string { $ length = mb_strlen ( $ str ) ; if ( $ length <= $ maxLength ) { return $ str ; } $ endPos = $ maxPos = $ maxLength - mb_strlen ( $ ellipsis ) ; if ( $ needle !== "" ) { $ offset = $ maxPos - $ length ; $ endPos = mb_strrpos ( $ str , $ needle , $ offset ) ; if ( $ endPos === false ) { $ endPos = $ maxPos ; } } return mb_substr ( $ str , 0 , $ endPos ) . $ ellipsis ; }
Summarize a string to a given max length

@param string $str The value to summarize
@param int $maxLength The max number of characters to return
@param string $ellipsis A string to append to the result
@param string $needle If provided, attempt to split on this string
Summarize a string to a given max length