Reputation: 952
I have this JS code which I think is equivalent to the PHP str_word_count()
function, but still they return different words counts.
My JS code:
//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;
outputs: 300
My PHP code:
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")
outputs: 290
What does the PHP str_word_count()
doesn't count as word, which my JS code does? Also can you suggest what to change so I can get same count for JS and PHP code?
Upvotes: 6
Views: 1153
Reputation: 5484
As PHP manual for str_word_count says:
For the purpose of this function, 'word' is defined as a locale dependent string containing alphabetic characters, which also may contain, but not start with
'
and-
characters.
So integers are not counted as words. You have ten occurances of 2015
surrounded by spaces. If it's instantly followed with letters, it will still count them, for example: 2015Yes
.
You may want to read this question to also count numbers in PHP.
OR
remove numbers in JS.
words = words.replace(/[0-9]/gi,"");
As @Rizier123 pointed out, you can see all words with this:
print_r(str_word_count($string, 1));
Upvotes: 4