src/Util/Str.php
public static function shortenWordsByChar($input, $num_chars = 35, $hellip = '&nbsp;&hellip;')
    {
        if (strlen($input) <= $num_chars) {
            return $input;
        }

        $shortened = substr($input, 0, $num_chars);
        $shortened_words = array_filter(explode(' ', $shortened));
        end($shortened_words);
        $last_key = key($shortened_words);

        $words = explode(' ', $input);
        $words = array_slice($words, 0, $last_key + 1);
        if ($words[$last_key] !== $shortened_words[$last_key]) {
            unset($words[$last_key]);
        }
        return join(' ', $words) . $hellip;
    }
public static function shortenWordsByChar($input, $num_chars = 35, $hellip = '&nbsp;&hellip;')
    {
        if (strlen($input) <= $num_chars) {
            return $input;
        }

        $shortened = substr($input, 0, $num_chars);
        $shortened_words = array_filter(explode(' ', $shortened));
        end($shortened_words);
        $last_key = key($shortened_words);

        $words = explode(' ', $input);
        $words = array_slice($words, 0, $last_key + 1);
        if ($words[$last_key] !== $shortened_words[$last_key]) {
            unset($words[$last_key]);
        }
        return join(' ', $words) . $hellip;
    }
public static function shortenWordsByChar ( $ input , $ num_chars = 35 , $ hellip = '&nbsp;&hellip;' ) { if ( strlen ( $ input ) <= $ num_chars ) { return $ input ; } $ shortened = substr ( $ input , 0 , $ num_chars ) ; $ shortened_words = array_filter ( explode ( ' ' , $ shortened ) ) ; end ( $ shortened_words ) ; $ last_key = key ( $ shortened_words ) ; $ words = explode ( ' ' , $ input ) ; $ words = array_slice ( $ words , 0 , $ last_key + 1 ) ; if ( $ words [ $ last_key ] !== $ shortened_words [ $ last_key ] ) { unset ( $ words [ $ last_key ] ) ; } return join ( ' ' , $ words ) . $ hellip ; }
Shorten a string by character limit, preserving words
@param string $input
@param integer $num_chars
@param string $hellip
@return string
Shorten a string by character limit preserving words