PHP: str_word_counter does not count numerals

February 19, 2010 · Posted in PHP · Comment 

Realized that str_word_counter() does not count numerals (just numbers alone without alphabets) as a word.

The manual’s expression is:

mixed str_word_count ( string $string [, int $format = 0 [, string $charlist ]] )

If we supply “ab cd ef”, then the function returns 3, and if we supply “11 22 ab”, then it would be 1 since the function just ignores the numerals only value. The workaround (although it is not a bug or anything. It is a feature.) is to use the “$charlist”.

str_word_count ( string $string [, int $format = 0 , “0123456789” )

Then the numerals will be treated as letters.