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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了计算所有单词,包括php字符串中的数字脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
为了计算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.

使用它而不是爆炸(”,$string)的优点是它可以用于多行字符串以及制表符,而不仅仅是空格.缺点是速度较慢.

脚本宝典总结

以上是脚本宝典为你收集整理的计算所有单词,包括php字符串中的数字全部内容,希望文章能够帮你解决计算所有单词,包括php字符串中的数字所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。