From a5ead744b3e6a77d3853b0156a263a3a3d2f179e Mon Sep 17 00:00:00 2001 From: Jeroen van Rensen Date: Wed, 14 Apr 2021 20:55:43 +0200 Subject: [PATCH 1/3] Add a wordCount() string helper --- src/Illuminate/Support/Str.php | 11 +++++++++++ src/Illuminate/Support/Stringable.php | 10 ++++++++++ tests/Support/SupportStrTest.php | 6 ++++++ tests/Support/SupportStringableTest.php | 6 ++++++ 4 files changed, 33 insertions(+) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index 1e6b849cc8a9..fa0a8df8b2b9 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -766,6 +766,17 @@ public static function ucfirst($string) return static::upper(static::substr($string, 0, 1)).static::substr($string, 1); } + /** + * Get the amount of words a string contains. + * + * @param string $string + * @return int + */ + public static function wordCount($string) + { + return str_word_count($string); + } + /** * Generate a UUID (version 4). * diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index 79d5a9160215..d4ba4ca2128f 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -756,6 +756,16 @@ public function words($words = 100, $end = '...') return new static(Str::words($this->value, $words, $end)); } + /** + * Get the amount of words a string contains. + * + * @return int + */ + public function wordCount() + { + return str_word_count($this->value); + } + /** * Dump the string. * diff --git a/tests/Support/SupportStrTest.php b/tests/Support/SupportStrTest.php index f120708968dc..d8c744b37c70 100755 --- a/tests/Support/SupportStrTest.php +++ b/tests/Support/SupportStrTest.php @@ -494,6 +494,12 @@ public function testPadRight() $this->assertSame('Alien ', Str::padRight('Alien', 10)); } + public function testWordCount() + { + $this->assertEquals(2, Str::wordCount('Hello, world!')); + $this->assertEquals(10, Str::wordCount('Hi, this is my first contribution to the Laravel framework.')); + } + public function validUuidList() { return [ diff --git a/tests/Support/SupportStringableTest.php b/tests/Support/SupportStringableTest.php index be88413d750a..70916a0458b9 100644 --- a/tests/Support/SupportStringableTest.php +++ b/tests/Support/SupportStringableTest.php @@ -608,4 +608,10 @@ public function testRepeat() $this->assertSame('aaaaa', (string) $this->stringable('a')->repeat(5)); $this->assertSame('', (string) $this->stringable('')->repeat(5)); } + + public function testWordCount() + { + $this->assertEquals(2, $this->stringable('Hello, world!')->wordCount()); + $this->assertEquals(10, $this->stringable('Hi, this is my first contribution to the Laravel framework.')->wordCount()); + } } From 976a8e1808d916d13919b2df47c3f8256cfcffe3 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 14 Apr 2021 14:33:35 -0500 Subject: [PATCH 2/3] Update Str.php --- src/Illuminate/Support/Str.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index fa0a8df8b2b9..37e3fd082956 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -767,7 +767,7 @@ public static function ucfirst($string) } /** - * Get the amount of words a string contains. + * Get the number of words a string contains. * * @param string $string * @return int From 6423cdd740f28fe5a15004863468b41863cc9922 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 14 Apr 2021 14:33:49 -0500 Subject: [PATCH 3/3] Update Stringable.php --- src/Illuminate/Support/Stringable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index d4ba4ca2128f..290914bc32a1 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -757,7 +757,7 @@ public function words($words = 100, $end = '...') } /** - * Get the amount of words a string contains. + * Get the number of words a string contains. * * @return int */