How to count words in a string in PHP

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

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));

The output is:

Array ( [0] => aaa [1] => bbb [2] => ccc [3] => ddd )

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'));

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 )

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'));

The output is:

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

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

Did you find this article valuable?

Support Yongyao Yan by becoming a sponsor. Any amount is appreciated!