src/Traits/Functions.php
public static function truncate(string $text, int $limit = 40): string
    {
        if (str_word_count($text, 0) > $limit) {
            $words = (array)str_word_count($text, 2);
            $pos = array_keys($words);
            $text = mb_substr($text, 0, $pos[$limit], 'UTF-8') . '...';
        }

        return $text;
    }
public static function truncate(string $text, int $limit = 40): string
    {
        if (str_word_count($text, 0) > $limit) {
            $words = (array)str_word_count($text, 2);
            $pos = array_keys($words);
            $text = mb_substr($text, 0, $pos[$limit], 'UTF-8') . '...';
        }

        return $text;
    }
public static function truncate ( string $ text , int $ limit = 40 ) : string { if ( str_word_count ( $ text , 0 ) > $ limit ) { $ words = ( array ) str_word_count ( $ text , 2 ) ; $ pos = array_keys ( $ words ) ; $ text = mb_substr ( $ text , 0 , $ pos [ $ limit ] , 'UTF-8' ) . '...' ; } return $ text ; }
Truncate string based on count of words

@usage $string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque et mi orci.';
__::truncate($string);
>> 'Lorem ipsum dolor sit amet, consectetur...'

@param string  $text  text to truncate
@param integer $limit limit of words

@return string
Truncate string based on count of words