From ba4fd3fab00123ecd92871870d0af5730748d34e Mon Sep 17 00:00:00 2001 From: Eric Barnes Date: Sun, 9 Oct 2011 23:23:45 -0400 Subject: [PATCH 1/3] Added str::limit and str::limit_words. Unfortunately php doesn't include mb_str_word_count. --- laravel/str.php | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/laravel/str.php b/laravel/str.php index 10066696..b01d397b 100644 --- a/laravel/str.php +++ b/laravel/str.php @@ -98,6 +98,55 @@ public static function length($value) return strlen($value); } + /** + * Limit the number of chars in a string + * + * + * // Limit the characters + * echo Str::limit_chars('taylor otwell', 3); + * results in 'tay...' + * + * + * @param string $value + * @param int $length + * @param string $end + * @return string + */ + public static function limit($value, $length = 100, $end = '...') + { + if (static::length($value) <= $length) return $value; + + if (function_exists('mb_substr')) + { + return mb_substr($value, 0, $length).$end; + } + + return substr($value, 0, $length).$end; + } + + /** + * Limit the number of words in a string + * + * + * // Limit the words + * echo Str::limit_chars('This is a sentence.', 3); + * results in 'This is a...' + * + * + * @param string $value + * @param int $length + * @param string $end + * @return string + */ + public static function limit_words($value, $length = 100, $end = '...') + { + $count = str_word_count($value,1); + + if ($count <= $length) return $value; + + return implode(' ',array_slice($count,0,$length)).$end; + } + /** * Convert a string to 7-bit ASCII. * From 212b3490267be2ca8f8b7737751999779002cfca Mon Sep 17 00:00:00 2001 From: Eric Barnes Date: Tue, 11 Oct 2011 00:24:42 -0400 Subject: [PATCH 2/3] Added Str tests. --- tests/Cases/StrTest.php | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 tests/Cases/StrTest.php diff --git a/tests/Cases/StrTest.php b/tests/Cases/StrTest.php new file mode 100644 index 00000000..8f333186 --- /dev/null +++ b/tests/Cases/StrTest.php @@ -0,0 +1,38 @@ +assertEquals('something', Laravel\Str::lower('SomeThing')); + $this->assertEquals('τάχιστη', Laravel\Str::lower('ΤΆΧΙΣΤΗ')); + } + + public function test_upper() + { + $this->assertEquals('SPEAK LOUDER', Laravel\Str::upper('speak louder')); + $this->assertEquals('ΤΆΧΙΣΤΗ', Laravel\Str::upper('Τάχιστη')); + } + + public function test_title() + { + $this->assertEquals('This Is A Test', Laravel\Str::title('this is a test')); + $this->assertEquals('Τάχιστη Τάχιστη', Laravel\Str::title('τάχιστη τάχιστη')); + } + + public function test_length() + { + $this->assertEquals(4, Laravel\Str::length('four')); + $this->assertEquals(7, Laravel\Str::length('τάχιστη')); + } + + public function test_ascii() + { + $this->assertEquals('Deuxieme Article', Laravel\Str::ascii('Deuxième Article')); + } + + public function test_random() + { + $this->assertEquals(5, strlen(Laravel\Str::random(5))); + } +} \ No newline at end of file From aa607c99628632ce2850598f66c8582b5d7b1a57 Mon Sep 17 00:00:00 2001 From: Eric Barnes Date: Tue, 11 Oct 2011 16:35:54 -0400 Subject: [PATCH 3/3] Added unit tests for Str::limit and limit_words. Also fixed bug in limit with multibyte characters. --- laravel/str.php | 2 +- tests/Cases/StrTest.php | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/laravel/str.php b/laravel/str.php index b01d397b..a0d23407 100644 --- a/laravel/str.php +++ b/laravel/str.php @@ -118,7 +118,7 @@ public static function limit($value, $length = 100, $end = '...') if (function_exists('mb_substr')) { - return mb_substr($value, 0, $length).$end; + return mb_substr($value, 0, $length, Config::get('application.encoding')).$end; } return substr($value, 0, $length).$end; diff --git a/tests/Cases/StrTest.php b/tests/Cases/StrTest.php index 8f333186..55b47830 100644 --- a/tests/Cases/StrTest.php +++ b/tests/Cases/StrTest.php @@ -35,4 +35,17 @@ public function test_random() { $this->assertEquals(5, strlen(Laravel\Str::random(5))); } + + public function test_limit() + { + $this->assertEquals('Thi...', Laravel\Str::limit('This is a string of text', 3, '...')); + $this->assertEquals('This is ', Laravel\Str::limit('This is a string of text', 7, ' ')); + $this->assertEquals('τάχ', Laravel\Str::limit('τάχιστη', 3, '')); + } + + 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 string ', Laravel\Str::limit_words('This is a string of text', 4, ' ')); + } } \ No newline at end of file