diff --git a/laravel/str.php b/laravel/str.php index 8f46282c..0b6b5cfa 100644 --- a/laravel/str.php +++ b/laravel/str.php @@ -123,6 +123,34 @@ public static function limit($value, $limit = 100, $end = '...') return substr($value, 0, $limit).$end; } + /** + * Limit the number of words in a string. + * + * + * // Returns "This is a..." + * echo Str::words('This is a sentence.', 3); + * + * // Limit the number of words and append a custom ending + * echo Str::words('This is a sentence.', 3, '---'); + * + * + * @param string $value + * @param int $words + * @param string $end + * @return string + */ + public static function words($value, $words = 100, $end = '...') + { + preg_match('/^\s*+(?:\S++\s*+){1,'.$words.'}/', $value, $matches); + + if (static::length($value) == static::length($matches[0])) + { + $end = ''; + } + + return rtrim($matches[0]).$end; + } + /** * Get the singular form of the given word. * @@ -166,34 +194,6 @@ public static function plural($value, $count = 2) return (ctype_upper($value[0])) ? static::title($plural) : $plural; } - /** - * Limit the number of words in a string. - * - * - * // Returns "This is a..." - * echo Str::words('This is a sentence.', 3); - * - * // Limit the number of words and append a custom ending - * echo Str::words('This is a sentence.', 3, '---'); - * - * - * @param string $value - * @param int $words - * @param string $end - * @return string - */ - public static function words($value, $words = 100, $end = '...') - { - preg_match('/^\s*+(?:\S++\s*+){1,'.$words.'}/', $value, $matches); - - if (static::length($value) == static::length($matches[0])) - { - $end = ''; - } - - return rtrim($matches[0]).$end; - } - /** * Generate a URL friendly "slug" from a given string. *