PHP比str\u word\u count更能计算单词数
因为我读到str_word_count是有缺陷的,所以我寻找了一个替代解决方案,并发现了以下问题,除了一个问题外,这些问题总体上都很有效PHP比str\u word\u count更能计算单词数,php,function,count,word-count,Php,Function,Count,Word Count,因为我读到str_word_count是有缺陷的,所以我寻找了一个替代解决方案,并发现了以下问题,除了一个问题外,这些问题总体上都很有效 function count_words($text) { //it removes html tags $text = preg_replace('/<[^>]*>/', '', $text); //it removes html space code $text = preg_replace(array
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));
}
它将计算7个单词,而不是6个
有没有可能从这个字数中排除像-这样的单个字符?我只需要计算字数:
$count = preg_match_all("/[\w']+/", $text);
要获得删除HTML标记和HTML实体的功能,请执行以下操作:
$count = preg_match_all("/[\w']+/", html_entity_decode(strip_tags($text), ENT_QUOTES));
也许更好的办法是把你认为构成一个词的东西包括进去。添加\w
未涵盖的任何内容。i
使其不区分大小写:
$count = preg_match_all("/[a-z']+/i", html_entity_decode(strip_tags($text), ENT_QUOTES));
好奇你在哪里读到的
str\u word\u count
是有缺陷的。我自己在一个更大的文本上测试了它,它没有给我准确的字数,比如微软的word。这里也提到了缺陷很好,谢谢!有没有办法把“不”算作一个词而不是两个词?太好了。是否也可以排除数字?:)字数统计用于翻译,以便人类翻译人员知道有多少单词需要翻译。由于数字/数字不需要翻译,我不想把它们算作文字。对不起。我真的不明白。仅使用最后一行几乎统计所有字符。或者它应该与上面的线条结合使用?如果是,怎么做?对不起,我不知怎么删除了+
。
$count = preg_match_all("/[a-z']+/i", html_entity_decode(strip_tags($text), ENT_QUOTES));