Skip to content

Commit b8e2534

Browse files
committed
up: add new method for count string word
1 parent e4f5fec commit b8e2534

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/Str/StringHelper.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@
3535
use function mb_substr;
3636
use function preg_match;
3737
use function preg_match_all;
38+
use function preg_split;
3839
use function random_bytes;
3940
use function str_pad;
4041
use function str_repeat;
4142
use function str_replace;
43+
use function str_word_count;
4244
use function strip_tags;
4345
use function strlen;
4446
use function strpos;
@@ -256,7 +258,7 @@ public static function format($str, array $replaceParams = [], array $pregParams
256258
*
257259
* @return string 格式化后的字符串
258260
*/
259-
public static function wordFormat($keyword): string
261+
public static function wordFormat(string $keyword): string
260262
{
261263
// 将全角角逗号换为空格
262264
$keyword = str_replace(['', ','], ' ', $keyword);
@@ -313,6 +315,26 @@ public static function deleteStripSpace($fileName, $type = 0)
313315
/// Other
314316
////////////////////////////////////////////////////////////
315317

318+
/**
319+
* @param string $str
320+
*
321+
* @return int
322+
*/
323+
public static function wordCount(string $str): int
324+
{
325+
return str_word_count($str);
326+
}
327+
328+
/**
329+
* @param string $str
330+
*
331+
* @return int
332+
*/
333+
public static function utf8WordCount(string $str): int
334+
{
335+
return count(preg_split('~[^\p{L}\p{N}\']+~u',$str));
336+
}
337+
316338
/**
317339
* @param string $arg
318340
*

0 commit comments

Comments
 (0)