我有这个JS代码,我认为它等同于PHP str_word_count()
函数,但它们仍会返回不同的字数。
我的JS代码:
//element f9 value is:
"Yes, for all people asking ? ? ? their selfs Have you ever dreamed to
visite World Travel Market 2015 ? we can confirm that now it is a great
time to go to London for World Travel Market 2015Yes, for all people
asking their selfs Have you ever dreamed to visite World Travel Market 2015 ?
we can confirm that now it is a great time to go to London for World
Travel Market 2015Yes, for all people asking their selfs Have you ever
dreamed to visite World Travel Market 2015 ? we can confirm that now it is
a great time to go to London for World 2015Yes, for all people asking
their selfs Have you ever dreamed to visite World 2015 we can confirm that
now it is a great time to go to London for World Travel Market 2015Yes, for
all people asking their selfs Have you ever dreamed to visite World Travel
Market 2015 ? we can confirm that now it is a great time to go to London
for World 2015Yes, for all people asking their selfs Have you ever dreamed
to visite World Travel Market 2015 ? we can confirm that now it is a great
time to go to London for World Travel Market 2015Yes, for all people asking
their selfs Have you ever dreamed to visite World 2015 we can confirm that
now it is a great time to go to London for World 2015Yes, for all people
asking their selfs Have you ever dreamed to visite World Travel Market
2015 ? we can confirm that now it is a great time to go to London for World
2015Yes, for all people asking their selfs Have you ever dreamed to visite
World 2015 ? we can confirm that now it is a great time to go to London for
World 2015"
var words = document.getElementById("f9").value.replace(/([(\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!<>\|\:])/g, '');
words = words.replace(/(^\s*)|(\s*$)/gi,"");
words = words.replace(/[ ]{2,}/gi," ");
words = words.replace(/\n /,"\n");
words = words.split(' ').length;
输出:300
我的PHP代码:
str_word_count("Yes, for all people asking ? ? ? their selfs Have you ever dreamed to
visite World Travel Market 2015 ? we can confirm that now it is a great
time to go to London for World Travel Market 2015Yes, for all people
asking their selfs Have you ever dreamed to visite World Travel Market 2015 ?
we can confirm that now it is a great time to go to London for World
Travel Market 2015Yes, for all people asking their selfs Have you ever
dreamed to visite World Travel Market 2015 ? we can confirm that now it is
a great time to go to London for World 2015Yes, for all people asking
their selfs Have you ever dreamed to visite World 2015 we can confirm that
now it is a great time to go to London for World Travel Market 2015Yes, for
all people asking their selfs Have you ever dreamed to visite World Travel
Market 2015 ? we can confirm that now it is a great time to go to London
for World 2015Yes, for all people asking their selfs Have you ever dreamed
to visite World Travel Market 2015 ? we can confirm that now it is a great
time to go to London for World Travel Market 2015Yes, for all people asking
their selfs Have you ever dreamed to visite World 2015 we can confirm that
now it is a great time to go to London for World 2015Yes, for all people
asking their selfs Have you ever dreamed to visite World Travel Market
2015 ? we can confirm that now it is a great time to go to London for World
2015Yes, for all people asking their selfs Have you ever dreamed to visite
World 2015 ? we can confirm that now it is a great time to go to London for
World 2015")
输出:290
PHP str_word_count()
什么不算作word,我的JS代码呢?你也可以建议改变什么,这样我可以获得JS和PHP代码的相同数量吗?
答案 0 :(得分:4)
正如str_word_count的PHP手册所说:
出于此功能的目的,&#39; word&#39;被定义为依赖于语言环境的字符串包含字母字符,它也可能包含但不以
'
和-
字符开头。
所以整数不算作单词。你有10个2015
被空格包围。如果它立即跟着字母,它仍然会计算它们,例如:2015Yes
。
您可能需要阅读this question以计算PHP中的数字。
OR
删除JS中的数字。
words = words.replace(/[0-9]/gi,"");
正如@Rizier123指出的那样,您可以看到所有单词:
print_r(str_word_count($string, 1));