Why does the php str_word_count () function return a different counter from the js equivalent?

I have this JS code, which I think is equivalent to the PHP str_word_count() function, but still they return different numbers of words.

My JS code is:

 //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

My PHP code is:

 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 PHP str_word_count() do except for the word that my JS code does? Also can you suggest what to change so that I can get the same score for JS and PHP code?

+6
source share
1 answer

As the PHP manual for str_word_count says:

For this function, the word "word" is defined as a language-dependent string containing alphabetic characters , which may also contain, but not begin with the characters ' and - .

Thus, integers are not considered words. You have ten 2015 events surrounded by spaces. If it is immediately followed by letters, it will still count them, for example: 2015Yes .

You might want to read this question to also count numbers in PHP.

OR

remove numbers in js.

 words = words.replace(/[0-9]/gi,""); 

As @ Rizier123 noted, you can see all the words as follows:

 print_r(str_word_count($string, 1)); 
+4
source

All Articles