6

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?

1
  • Do: print_r(str_word_count($string, 1)); and look for yourself what all the words are. Commented Aug 12, 2015 at 7:15

1 Answer 1

4

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));
Sign up to request clarification or add additional context in comments.

8 Comments

Rene, if you see at js script you will notice that ? mark is not counted by js also, so this is not the reason. I can see the word value clean of ? before getting its lenght
@Europeuser Yes, you are right. Didn't notice that.
Yeah. It was integers. 2015 surrounded with spaces. Ten occurances.
Oh I see now.. Any suggestions how to avoid this difference? Should I include 1,2,3 .. 0 to replace ?
Yes. You could do words = words.replace(/[0-9]/gi,"");.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.