Thursday, October 22, 2009

Is there a javascript (or some other) function i can use to count the number of words on a web page?

I don%26#039;t want to count the words in a text box. I want to pass an http reference and return the number of words on the page.



Is there a javascript (or some other) function i can use to count the number of words on a web page?





I can%26#039;t think of an easy way to do it using javascript, but if you can use a server side script it shouldn%26#039;t be too difficult.



In PHP it would be:



$string = %26quot;a lot of text, the content of your page or whatever%26quot;;



$string = striptags( $string );//remove any html tags that would throw this off



$string = preg_replace(%26quot;/\w+/%26quot;, %26quot; %26quot;, $string);//change all white space characters, or groups of white space to one space



$string = trim($string);//remove any leading or trailing white space



$array = explode(%26quot; %26quot;,$string);//change the string to an array broken on spaces



$wordCount = count($array);



Is there a javascript (or some other) function i can use to count the number of words on a web page?



nope, you would have to use the innerText property and analise the string yourself, maybe use the split function to count words

No comments:

Post a Comment

Blog Archive