计算php字符串中包括数字在内的所有单词

计算php字符串中包括数字在内的所有单词,php,Php,要计算php字符串中的单词,通常我们可以使用str\u word\u count,但我认为这并不总是一个好的解决方案 好例子: -->输出 坏例子: -->输出: 是否有一个好的预定义函数来正确执行此操作,或者我必须使用preg_match()?您可以始终按空格分割字符串并计算结果: $res = preg_split('/\s+/', $input); $count = count($res); 用你的绳子 "The example number 2 is a bad example it

要计算php字符串中的单词,通常我们可以使用str\u word\u count,但我认为这并不总是一个好的解决方案

好例子: -->输出

坏例子: -->输出:


是否有一个好的预定义函数来正确执行此操作,或者我必须使用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)相比,使用此选项的优势在于它可以处理多行字符串以及制表符,而不仅仅是空格。缺点是速度较慢。

使用
计数(explode(“”,$var))

以下使用和将回显:

The number 1 in this line will counted and it contains the following count 8 此行中的数字1将被计数,它包含以下计数8 PHP:



编辑:

旁注:
可以修改正则表达式以接受标准集中未包含的其他字符

<?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.";

?>

答案:


你也可以使用下面的代码,它为我工作

    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.~!@#$%^&*()-_=+{}[]\|;:?/<>.,'));
print\r(str\u word\u 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));

}

计算字符串中单词的最广泛方法是使用任何类型的空格进行拆分:

count(preg_split('~\s+~u', trim($text)))
此处,
“~\s+~u”
使用任意1个或多个Unicode空白字符拆分整个文本

缺点是
被认为是一个单词。

<强>如果你想计算字母和数字单词< /强>(即,仅由字母或数字组成的字符串),你应该考虑< <代码> PRGGyMatHyALL >

if (preg_match_all('~[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?|\d+|(?>\p{L}\p{M}*+)+~u', $text, $matches)) {
    return count($matches[0]);
}
请参阅和:

[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)
正则表达式是众所周知的,
(?>\p{L}\p{M}*+
匹配任何一个或多个字母(
\p{L}
),每个字母后面都可以有任意数量的变音符号(
\p{M}>)

正则表达式详细信息

  • [-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)
    -可选的
    -
    +
    ,0+ASCII数字,可选的
    ,1+ASCII数字,可选的
    e
    e
    序列,可选的
    -
    ,然后是1+ASCII数字
  • |
    -或
  • \d+
    -任意一个或多个Unicode数字
  • |
    -或
  • (?>\p{L}\p{M}*+)+
    -1次或多次出现任何Unicode字母,后跟任何0+变音符号
如果您只想计算以任意顺序混合的数字和字母(带变音符号)组成的文本块,您也可以使用

'~[\p{N}\p{L}\p{M}]+~u'

请参阅,
\p{M}
匹配变音符号,
\p{N}
匹配数字,
\p{L}
匹配字母。

poss使用
空白
计算出有多少?您可以使用
$words=explode(''.$var)
@TomKriek也没有这个糟糕的解决方案,因为我的输入在两个字之间可能有一个以上的空格。
打印计数(explode(“”,“hallo world”)//输出2打印计数(爆炸(“”,“哈罗世界”)
//输出3sorry,但在第二个示例中,额外的blanc已从我之前的注释中删除。然后,尝试使用此
$words=array\u过滤器(explode(“”,$var))explode(“”,$string)并不总是有效的,因为我的数据存储在数据库中,我无法判断每个单词之间是否有一个或多个空格,但第一个解决方案总是有效的。这也是一个糟糕的解决方案,因为我的输入可以在两个单词之间有多个空格。打印计数(explode(“”,“hallo world”)//输出2打印计数(爆炸(“”,“哈罗世界”)//输出3 PS第二个字符串中的第二个空格将自动从my commentworks中删除,但例外情况是,如果在单词之间添加一些额外的空格,则计数将被删除increased@Amani我懂了。谢谢你,我很高兴看到你从Aleks那里得到了你的解决方案,干杯:)@Amani我做了一个编辑,在这个版本中,额外的空格和标点将不被计算在内。谢谢你的解决方案+1为Edit@Amani非常欢迎。请保留已接受的答案。我只是想让我的答案更好,因为我最初没有考虑可能的额外空格(和标点符号)。干杯和感谢:)我希望它也能为您服务。这是个好问题+1如果您正在使用该类。您将添加这个。
Array ( [0] => Hello [1] => world [2] => ! )
print_r(str_word_count($str, 1, '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));

}
count(preg_split('~\s+~u', trim($text)))
if (preg_match_all('~[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?|\d+|(?>\p{L}\p{M}*+)+~u', $text, $matches)) {
    return count($matches[0]);
}
$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
'~[\p{N}\p{L}\p{M}]+~u'