public static function cutLongStrings(string $sentence, int $max_length = 50, string $separator = ' ', string $postfix = '...'): string
    {
        // Return actual string if have nothing to cut
        if (mb_strlen($sentence, 'utf-8') <= $max_length) {
            return $sentence;
        }

        $words = mb_split("[" . $separator . "]+", $sentence);
        $res = '';
        $count_of_words = count($words);

        for ($i = 0; $i < $count_of_words; $i++) {
            if (mb_strlen($res . $words[$i] . ' ', 'utf-8') < $max_length) {
                // Add to returned string
                $res .= $words[$i] . ' ';
            } else {
                // Enough
                break;
            }
        }

        return mb_substr($res, 0, -1, 'utf-8') . $postfix;
    }
public static function cutLongStrings(string $sentence, int $max_length = 50, string $separator = ' ', string $postfix = '...'): string
    {
        // Return actual string if have nothing to cut
        if (mb_strlen($sentence, 'utf-8') <= $max_length) {
            return $sentence;
        }

        $words = mb_split("[" . $separator . "]+", $sentence);
        $res = '';
        $count_of_words = count($words);

        for ($i = 0; $i < $count_of_words; $i++) {
            if (mb_strlen($res . $words[$i] . ' ', 'utf-8') < $max_length) {
                // Add to returned string
                $res .= $words[$i] . ' ';
            } else {
                // Enough
                break;
            }
        }

        return mb_substr($res, 0, -1, 'utf-8') . $postfix;
    }
public static function cutLongStrings ( string $ sentence , int $ max_length = 50 , string $ separator = ' ' , string $ postfix = '...' ) : string { // Return actual string if have nothing to cut if ( mb_strlen ( $ sentence , 'utf-8' ) <= $ max_length ) { return $ sentence ; } $ words = mb_split ( "[" . $ separator . "]+" , $ sentence ) ; $ res = '' ; $ count_of_words = count ( $ words ) ; for ( $ i = 0 ; $ i < $ count_of_words ; $ i ++ ) { if ( mb_strlen ( $ res . $ words [ $ i ] . ' ' , 'utf-8' ) < $ max_length ) { // Add to returned string $ res .= $ words [ $ i ] . ' ' ; } else { // Enough break ; } } return mb_substr ( $ res , 0 , - 1 , 'utf-8' ) . $ postfix ; }
Cuts long sentences so only first few words are returned

@param string $sentence
@param int    $max_length
@param string $separator
@param string $postfix to add after sentence is cut

@return string
Cuts long sentences so only first few words are returned