要计算php字符串中的单词,通常我们可以使用str_word_count但我认为并不总是一个好的解决方案
很好的例子:
$var ="Hello world!";
echo str_word_count($str);
print_r(str_word_count($str, 1));
-->输出
2
Array ( [0] => Hello [1] => world )
不好的例子:
$var ="The example number 2 is a bad example it will not
count numbers and punctuations !!";
-->输出:
14
Array ( [0] => The [1] => example [2] => number [3] => is [4] => a
[5] => bad [6] => example [7] => it [8] => will [9] => not
[10] => count [11] => numbers [12] => and [13] => punctuations )
是否有一个好的预定义函数可以正确执行此操作,或者我必须使用 preg_match(( ?
您始终可以按空格拆分字符串并计算结果:
$res = preg_split('/'s+/', $input);
$count = count($res);
用你的绳子
"The example number 2 is a bad example it will not
count numbers and punctuations !!"
此代码将生成16
。
使用它优于explode(' ', $string)
的优点是它将适用于多行字符串和制表符,而不仅仅是空格。 缺点是速度较慢。
下面使用 count()
和 explode()
,将回显:
此行中的数字 1 将被计数,它包含以下计数 8
.PHP:
<?php
$text = "The number 1 in this line will counted";
$count = count(explode(" ", $text));
echo "$text and it contains the following count $count";
?>
编辑:
旁注:
可以修改正则表达式以接受标准集中未包含的其他字符。
<?php
$text = "The numbers 1 3 spaces and punctuations will not be counted !! . . ";
$text = trim(preg_replace('/[^A-Za-z0-9'-]/', ' ', $text));
$text = preg_replace('/'s+/', ' ', $text);
// used for the function to echo the line of text
$string = $text;
function clean($string) {
return preg_replace('/[^A-Za-z0-9'-]/', ' ', $string);
}
echo clean($string);
echo "<br>";
echo "There are ";
echo $count = count(explode(" ", $text));
echo " words in this line, this includes the number(s).";
echo "<br>";
echo "It will not count punctuations.";
?>
字符串中单词的最广泛方法是使用任何类型的空格进行拆分:
count(preg_split('~'s+~u', trim($text)))
在这里,'~'s+~u'
用任何 1 个或多个 Unicode 空格字符拆分整个文本。
缺点是!!
被认为是一个词。
如果你想计算字母和数字单词(即仅由字母或数字组成的文本字符串(,你应该考虑一个preg_match_all
,比如
if (preg_match_all('~[-+]?[0-9]*'.?[0-9]+(?:[eE][-+]?[0-9]+)?|'d+|(?>'p{L}'p{M}*+)+~u', $text, $matches)) {
return count($matches[0]);
}
请参阅正则表达式演示和 PHP 演示:
$re = '~[-+]?[0-9]*'.?[0-9]+(?:[eE][-+]?[0-9]+)?|'d+|(?>'p{L}'p{M}*+)+~u';
$text = "The example number 2 is a bad example it will not 'ncount numbers and punctuations !! X is 2.5674.";
if (preg_match_all($re, $text, $matches)) {
echo count($matches[0]);
} // 18 in this string
正则表达式[-+]?[0-9]*'.?[0-9]+(?:[eE][-+]?[0-9]+)?
是一个众所周知的整数或浮点数正则表达式,(?>'p{L}'p{M}*+)+
匹配任何 1 个或多个字母 ('p{L}
(,每个字母后面都可以跟任何数量的变音符号 ('p{M}*+
(。
正则表达式详细信息
-
[-+]?[0-9]*'.?[0-9]+(?:[eE][-+]?[0-9]+)?
- 可选的-
或+
,0 + ASCII数字,可选的.
,1+ ASCII数字,可选的e
或E
序列,可选的-
或+
,然后是1+ ASCII数字 -
|
- 或 -
'd+
- 任意 1 个或多个 Unicode 数字 -
|
- 或 -
(?>'p{L}'p{M}*+)+
- 任何 Unicode 字母出现 1 次或多次,后跟任何 0+ 变音符号。
如果您只想计算仅由数字和字母(带变音符号(组成的文本块,则还可以使用
'~['p{N}'p{L}'p{M}]+~u'
查看另一个正则表达式演示,'p{M}
匹配变音符号,'p{N}
匹配数字,'p{L}
匹配字母。
使用 count(explode(' ', $var));
您也可以使用以下代码,它对我有用。
function get_num_of_words($string) {
$string = preg_replace('/'s+/', ' ', trim($string));
$words = explode(" ", $string);
return count($words);
}
$string="php string word count in simple way";
echo $count=get_num_of_words($string);
结果将是 7
这个问题很老,但我仍然在分享我为此采用的修复程序。
$str ="Hello world !";
// you can include allowed special characters as third param.
print_r(str_word_count($str, 1, '!'));
代码输出为
Array ( [0] => Hello [1] => world [2] => ! )
如果要包含更多单词,可以指定为第三个参数。
print_r(str_word_count($str, 1, '0..9.~!@#$%^&*()-_=+{}[]'|;:?/<>.,'));
从 0..9. 将包括所有数字,其他特殊字符单独插入。
只是一些改进您的解决方案
function stringWordNumberCount($text){
if (!$text) {
return 0;
}
//Clean the text to remove special character
$text = trim(preg_replace('/[^A-Za-z0-9'-]/', ' ', $text));
//Remove continus space on text
$text = trim( preg_replace('/'s+/', ' ',$text));
//count space
return count(explode(' ', $text));
}
ans:
function limit_text($text, $limit) {
if(str_word_count($text, 0) > $limit) {
$words = str_word_count($text, 2);
$pos = array_keys($words);
$text = substr($text, 0, $pos[$limit]) . '...';
}
return $text;
}