Commit 8f8fa097 by Taylor Otwell

Merge pull request #811 from xsbeats/feature/str_limit_exact

Feature: Str::limit_exact    limits a string including its custom ending to a specified length
parents c7679baf e9e8a5b3
......@@ -25,6 +25,7 @@ The **Str** class also provides three convenient methods for manipulating string
#### Limiting the number of characters in a string:
echo Str::limit($string, 10);
echo Str::limit_exact($string, 10);
#### Limiting the number of words in a string:
......
......@@ -131,6 +131,31 @@ class Str {
}
/**
* Limit the number of chracters in a string including custom ending
*
* <code>
* // Returns "Taylor..."
* echo Str::limit_exact('Taylor Otwell', 9);
*
* // Limit the number of characters and append a custom ending
* echo Str::limit_exact('Taylor Otwell', 9, '---');
* </code>
*
* @param string $value
* @param int $limit
* @param string $end
* @return string
*/
public static function limit_exact($value, $limit = 100, $end = '...')
{
if (static::length($value) <= $limit) return $value;
$limit -= static::length($end);
return static::limit($value, $limit, $end);
}
/**
* Limit the number of words in a string.
*
* <code>
......
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