David Nguyen
David Nguyen

Reputation: 8528

What's throwing off my str_word_count?

I'm using PHP's function to count the number of words from a textarea via POST...

The issue is that if I do a post back to my file and output the word count it is different than if I copy and paste the same text into my PHP script to evaluate the word count.

What is throwing off the number? There is difference of 6 words, incidentally there are 6 double line breaks in the textarea as well.

How do I minimize this difference?

Upvotes: 0

Views: 958

Answers (2)

If your line breaks are in HTML-form, you could use something like strip_tags()

If they aren't, I suspect an issue with encoding. Maybe an combination of stripslashes, utf8_encode or utf8_decode could solve this wrong counted words.

As an last resort you could use some regular expression to filter anything but [a-zA-Z] and spaces.

Upvotes: 0

John Fable
John Fable

Reputation: 1091

You could remove the line breaks and tags altogether:

str_word_count(str_replace('<br>', '', nl2br(strip_tags($data))));

Or I guess this is better:

str_word_count(strip_tags(nl2br($data)));

Upvotes: 2

Related Questions