Skip to content

Commit 097a364

Browse files
Andrey Helldartaylorotwell
andauthored
[9.x] Added character expansion parameter for searching in Str::wordCount() method (#41642)
* [9.x] Added character expansion parameter for searching in `Str::wordCount()` method * Update Str.php Co-authored-by: Taylor Otwell <[email protected]>
1 parent fb8d38c commit 097a364

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/Illuminate/Support/Str.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,11 +1020,12 @@ public static function ucsplit($string)
10201020
* Get the number of words a string contains.
10211021
*
10221022
* @param string $string
1023+
* @param string|null $characters
10231024
* @return int
10241025
*/
1025-
public static function wordCount($string)
1026+
public static function wordCount($string, $characters = null)
10261027
{
1027-
return str_word_count($string);
1028+
return str_word_count($string, 0, $characters);
10281029
}
10291030

10301031
/**

tests/Support/SupportStrTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,15 @@ public function testWordCount()
719719
{
720720
$this->assertEquals(2, Str::wordCount('Hello, world!'));
721721
$this->assertEquals(10, Str::wordCount('Hi, this is my first contribution to the Laravel framework.'));
722+
723+
$this->assertEquals(0, Str::wordCount('мама'));
724+
$this->assertEquals(0, Str::wordCount('мама мыла раму'));
725+
726+
$this->assertEquals(1, Str::wordCount('мама', 'абвгдеёжзийклмнопрстуфхцчшщъыьэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ'));
727+
$this->assertEquals(3, Str::wordCount('мама мыла раму', 'абвгдеёжзийклмнопрстуфхцчшщъыьэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ'));
728+
729+
$this->assertEquals(1, Str::wordCount('МАМА', 'абвгдеёжзийклмнопрстуфхцчшщъыьэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ'));
730+
$this->assertEquals(3, Str::wordCount('МАМА МЫЛА РАМУ', 'абвгдеёжзийклмнопрстуфхцчшщъыьэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ'));
722731
}
723732

724733
public function validUuidList()

0 commit comments

Comments
 (0)