src/Viserio/Component/Support/Str.php
public static function words(string $value, int $words = 100, string $end = '...'): string
    {
        \preg_match('/^\s*+(?:\S++\s*+){1,' . $words . '}/u', $value, $matches);

        if (! isset($matches[0]) || \mb_strlen($value) === \mb_strlen($matches[0])) {
            return $value;
        }

        return \rtrim($matches[0]) . $end;
    }
public static function words(string $value, int $words = 100, string $end = '...'): string
    {
        \preg_match('/^\s*+(?:\S++\s*+){1,' . $words . '}/u', $value, $matches);

        if (! isset($matches[0]) || \mb_strlen($value) === \mb_strlen($matches[0])) {
            return $value;
        }

        return \rtrim($matches[0]) . $end;
    }
Limit the number of words in a string.

@param string $value
@param int    $words
@param string $end

@return string