DEV Community

Cover image for How to count words in a string in PHP
Yongyao Yan
Yongyao Yan

Posted on • Originally published at codebilby.com

How to count words in a string in PHP

By using the function str_word_count(), we can get information about words used in a string.

Get the number of words found in a string

To find out how many words in a string, you can do like this:

$str = "aaa bbb ccc 123 ddd";

print_r(str_word_count($str, 0)); // 4
Enter fullscreen mode Exit fullscreen mode

In this example, the function finds 4 words in the string $str and the output is 4. The second parameter in the function str_word_count() specifies the return value of the function. Here, the value 0 specifies returning the number of words in the input string.

Get an array containing all the words found inside the string

If you set the second parameter's value to be 1, the function str_word_count() will return an array with all the words in the string.

$str = "aaa bbb ccc 123 ddd";

print_r(str_word_count($str, 1));
Enter fullscreen mode Exit fullscreen mode

The output is:

Array ( [0] => aaa [1] => bbb [2] => ccc [3] => ddd )
Enter fullscreen mode Exit fullscreen mode

If you want 123 to be considered as a 'word' and extracted from the string, relevant characters need to be specified in the third parameter of the function.

print_r(str_word_count($str, 1, '0..9'));
Enter fullscreen mode Exit fullscreen mode

As shown in the above code snippet, because the number range, i.e. from 0 to 9, is listed in the third parameter, the number 123 is considered as a 'word'. The output is:

Array ( [0] => aaa [1] => bbb [2] => ccc [3] => 123 [4] => ddd ) 
Enter fullscreen mode Exit fullscreen mode

If you need the numeric position of each word inside the string, you just need to change the third parameter's value to be 2.

$str = "aaa bbb ccc 123 ddd";

print_r(str_word_count($str, 2, '0..9'));
Enter fullscreen mode Exit fullscreen mode

The output is:

Array ( [0] => aaa [4] => bbb [8] => ccc [12] => 123 [16] => ddd ) 
Enter fullscreen mode Exit fullscreen mode

The key of each array item is the numeric position of the relevant word in the string.

Thanks for reading!
To find more programming tutorials, please visit: CodeBilby.com

Discussion (1)

Collapse
moopet profile image
Ben Sinclair • Edited on

That second parameter is nasty. I would never expect a function explicitly called "word count" to return an array of words. It should only ever return the number of words it counts, and as such I'd never use that parameter.

Unfortunately, if you want to specify the definition of "word", you need the third parameter, which means when you call the function, you need to include a meaningless "0" as the second argument. This is a great example of why people don't like PHP!

If I wanted to extract all the words, I'd either use preg_match_all or strtok, simply to help readability for anyone unfamiliar with what magic numbers to include. PHP doesn't even have constants defined for the different options, it's a bare integer :/

The randomness of the use of underscores in strtok compared to str_word_count, or the difference in naming compared to the character-based version of this function (count_chars) is another reason people don't like PHP. It's almost better to make your own properly-named wrappers for everything.