Str.php
public static function words(string $str, int $words = 100, string $encoding = null, string $end = '...') : string
    {
        $encoding = $encoding ?: static::encoding($str);

        preg_match('/^\s*+(?:\S++\s*+){1,'.$words.'}/u', $str, $matches);

        if (!isset($matches[0]) || mb_strlen($str, $encoding) === mb_strlen($matches[0], $encoding)) {
            return $str;
        }

        return rtrim($matches[0]).$end;
    }
public static function words(string $str, int $words = 100, string $encoding = null, string $end = '...') : string
    {
        $encoding = $encoding ?: static::encoding($str);

        preg_match('/^\s*+(?:\S++\s*+){1,'.$words.'}/u', $str, $matches);

        if (!isset($matches[0]) || mb_strlen($str, $encoding) === mb_strlen($matches[0], $encoding)) {
            return $str;
        }

        return rtrim($matches[0]).$end;
    }
public static function words ( string $ str , int $ words = 100 , string $ encoding = null , string $ end = '...' ) : string { $ encoding = $ encoding ? : static :: encoding ( $ str ) ; preg_match ( '/^\s*+(?:\S++\s*+){1,' . $ words . '}/u' , $ str , $ matches ) ; if ( ! isset ( $ matches [ 0 ] ) || mb_strlen ( $ str , $ encoding ) === mb_strlen ( $ matches [ 0 ] , $ encoding ) ) { return $ str ; } return rtrim ( $ matches [ 0 ] ) . $ end ; }
Limits the number of words in the given string.

@param   string      $str        The string to limit.
@param   int         $words      The maximal number of words to be contained in the string, not counting
the replacement.
@param   string|null $encoding   The encoding to use.
@param   string      $end        The replacement for the whole of the cut off string (if any).
@return  string                  The resulting string.
Limits the number of words in the given string .