How to count the number of words on the page?

Hello, tell me how to count the number of words on the page
For example, Closed (126 results)
Open (224 results)
Total (350)
June 3rd 19 at 18:53
2 answers
June 3rd 19 at 18:55
I think you need to use regular expressions and preg_match_all(). By the way, in php there is a very interesting function str_word_count(). It will work a little faster for the task to count the total number of words of text.
June 3rd 19 at 18:57
If js, I'd have written something like this (more pseudocode):
//get the whole page text
let all_text = document.body.innerText;
let word_count = 0;
let trigger = false;

for( let i =0; i < all_text.length; i++ ) {
 let c = all_text[i]; 
 if( typeof(c) == 'string' ) { //here need to check how the numbers
 trigger = true
 } else {
 if( trigger == true ) { //most likely not count the last word
 trigger = false;
word_count++;
}
}
}
console.log( 'word count:' + word_count );


Again, this is pseudocode, not even checked.
The code comments pointed out where it may be uncovered alleged errors, correct themselves.
document.body.the idea innerText will return the string and then iterate in a loop (all_text[i]) each letter of this string with the verification that the letter == 'string'? - Madge.Stroman81 commented on June 3rd 19 at 19:00
Yes - Eleanore.Park commented on June 3rd 19 at 19:03
well clear your script, you know better, in which cases the letter will not be equal to string, to fulfill the condition "false" =) - Madge.Stroman81 commented on June 3rd 19 at 19:06
Ahaha oops) of course it is necessary to compare with a space and line wraps) worked - Eleanore.Park commented on June 3rd 19 at 19:09

Find more questions by tags PHPJavaScript