Commit b73a60de authored by Taylor Otwell's avatar Taylor Otwell

rearrange str class.

parent ab17ea67
...@@ -123,6 +123,34 @@ class Str { ...@@ -123,6 +123,34 @@ class Str {
return substr($value, 0, $limit).$end; return substr($value, 0, $limit).$end;
} }
/**
* Limit the number of words in a string.
*
* <code>
* // 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, '---');
* </code>
*
* @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. * Get the singular form of the given word.
* *
...@@ -166,34 +194,6 @@ class Str { ...@@ -166,34 +194,6 @@ class Str {
return (ctype_upper($value[0])) ? static::title($plural) : $plural; return (ctype_upper($value[0])) ? static::title($plural) : $plural;
} }
/**
* Limit the number of words in a string.
*
* <code>
* // 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, '---');
* </code>
*
* @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. * Generate a URL friendly "slug" from a given string.
* *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment