PHP str_word_count() Function
Example
Count the words in the string "Hello World!":
Run Example »
Definition and Usage
The str_word_count() function counts the words in a string.
Syntax
str_word_count(
string,return,char
)
| Parameters | Description |
|---|---|
| string | Required. Specifies the string to check. |
| return | Optional. Specifies the return value of the str_word_count() function.
Possible values: |
Technical Details
| Return Value: | Returns a number or an array, depending on the selected return parameter was added. |
|---|---|
| PHP Version: | 4.3.0+ |
| Changelog: | In PHP 5.1, the char parameter was added. |
More examples
Example 1
Return an array containing the words in the string:
Run Example »
Example 2
Return an array where the keys are the positions of the words in the string and the values are the actual words:
Run Example »
Example 3
With and without the char parameter:
Run Example »
PHP String Reference Manual