Commit 17ea0936 authored by Taylor Otwell's avatar Taylor Otwell

fixed str test cases and str::limit function.

parent a6eaa069
...@@ -39,6 +39,7 @@ return array( ...@@ -39,6 +39,7 @@ return array(
'GET /' => function() 'GET /' => function()
{ {
return Laravel\Str::limit('This is a string of text', 3, '...');
return View::make('home.index'); return View::make('home.index');
}, },
......
...@@ -101,11 +101,8 @@ class Str { ...@@ -101,11 +101,8 @@ class Str {
/** /**
* Limit the number of characters in a string. * Limit the number of characters in a string.
* *
* Word integrity is preserved, so the number of characters in the
* truncated string will be rounded to the nearest word ending.
*
* <code> * <code>
* // Returns "Taylor..." * // Returns "Tay..."
* echo Str::limit('Taylor Otwell', 3); * echo Str::limit('Taylor Otwell', 3);
* *
* // Limit the number of characters and append a custom ending * // Limit the number of characters and append a custom ending
...@@ -113,17 +110,20 @@ class Str { ...@@ -113,17 +110,20 @@ class Str {
* </code> * </code>
* *
* @param string $value * @param string $value
* @param int $length * @param int $limit
* @param string $end * @param string $end
* @return string * @return string
*/ */
public static function limit($value, $limit = 100, $end = '...') public static function limit($value, $limit = 100, $end = '...')
{ {
if (static::length($value) < $limit) return $value; if (static::length($value) <= $limit) return $value;
$limit = preg_replace('/\s+?(\S+)?$/', '', substr($value, 0, $limit)); if (function_exists('mb_substr'))
{
return mb_substr($value, 0, $limit, Config::get('application.encoding')).$end;
}
return (static::length($limit) == static::length($value)) ? $value : $limit.$end; return substr($value, 0, $limit).$end;
} }
/** /**
......
...@@ -45,7 +45,7 @@ class StrTest extends PHPUnit_Framework_TestCase { ...@@ -45,7 +45,7 @@ class StrTest extends PHPUnit_Framework_TestCase {
public function test_limit_words() public function test_limit_words()
{ {
$this->assertEquals('This is a...', Laravel\Str::limit_words('This is a string of text', 3, '...')); $this->assertEquals('This is a...', Laravel\Str::words('This is a string of text', 3, '...'));
$this->assertEquals('This is a string&nbsp;', Laravel\Str::limit_words('This is a string of text', 4, '&nbsp;')); $this->assertEquals('This is a string&nbsp;', Laravel\Str::words('This is a string of text', 4, '&nbsp;'));
} }
} }
\ No newline at end of file
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