【发布时间】:2019-08-19 15:06:04
【问题描述】:
因为我读到 str_word_count 有缺陷,所以我搜索了一个替代解决方案并遇到了以下问题,除了一个问题之外,它总体上效果很好。
function count_words($text) {
//it removes html tags
$text = preg_replace('/<[^>]*>/', '', $text);
//it removes html space code
$text = preg_replace(array('/ /'), ' ', $text);
//it removes multiple spaces with single
$text = trim(preg_replace('!\s+!', ' ', $text));
return count(explode(' ', $text));
}
问题是它检测到一个破折号“-”作为一个词。
例子:
This is a title - Additional Info
它将计算 7 个单词而不是 6 个。
是否有可能从这个字数中排除单个字符,例如 -?
【问题讨论】:
-
好奇你在哪里读到
str_word_count有缺陷。 -
我自己在较大的文本上测试过它,但它并没有像 Microsoft word 那样给我准确的字数。而且这里也提到了缺陷stackoverflow.com/questions/4786802/…
标签: php function count word-count