swww
Forum Replies Created
-
Forum: Developing with WordPress
In reply to: WordPress Word Count FunctionI mentioned print_r() as the simplest way to see what’s going on in your code. You can use var_dump() as well. It will give you more comprehensive information. Try both function and see which of them you like more.
In order not to search for your desired number of words on the screen you might want to terminate your script immediately after print_r() (or var_dump()) is called, which can be done like this:
print_r($word_count); exit();This code can be put in functions.php, and its output as the last thing printed on the screen.
For more information and examples on print_r(), var_dump() and exit() you might want to visit php.net .
Forum: Developing with WordPress
In reply to: WordPress Word Count FunctionCount of words currently in the textarea control of the editor is found using not PHP but JavaScript. One way to obtain it by yourself is with WordCounter object as follows:
var counter = new window.wp.utils.WordCounter(); var str = document.getElementById('content').value counter.count(str)What’s worth noting is, stuff like years or dates are not considered words by count(). Hence, the above code will return 6 for a string like this: in 2020 the world will have ended
Similarly, the code below in PHP:
print_r(str_word_count(strip_tags("in 2020 the world will have ended")));will also return 6 as the number of words found (again years are not considered words by the function).
So, at a glance, for a simple string there is no difference between how count() and your code written in PHP find number of words. You might want to test how things stand for more complex, formatted content.
In conclusion, you probably want to trust count() rather than your PHP code on number of words in your post.
Forum: Developing with WordPress
In reply to: WordPress Word Count FunctionThe line below gives you total number of words in $content (tags stripped)
$word_count = str_word_count( strip_tags( $content ) );Now, if you:
print_r($word_count), you’ll see number of words in $content.
While speaking of wc utility, I had a command line program in my mind. It is available on Unix and Linux servers. You can try it out by logging in via ssh and typing something like:
wc -w <<< "your content"The above will give you number of words between quotes, which is 2.
Forum: Developing with WordPress
In reply to: WordPress Word Count FunctionHi,
Below is a bit of information on str_word_count().Read it carefully.
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, locale is important, make sure that receiver gets the right locale for the data you pass.
Secondly, you can see exactly what words are found in your data by passing 2 to str_word_count() as the second argument.
You can also compare output of the function with output given by command line program wc to see whether there are any discrepancies. wc -w gives number of words in a string.
Hope this helps.
[ Signature moderated ]- This reply was modified 8 years, 2 months ago by swww.
- This reply was modified 8 years, 2 months ago by swww.
- This reply was modified 8 years, 2 months ago by Steven Stern (sterndata).