Easy Tutorial
❮ Php Intval Function Php Ref Mysqli ❯

PHP str_word_count() Function

PHP String Reference Manual

Example

Count the number of words in the string "Hello World!":


Definition and Usage

The str_word_count() function counts the number of words in a string.


Syntax

Parameter Description
string Required. Specifies the string to check.
return Optional. Specifies the return value of the str_word_count() function. Possible values: 0 - Default. Returns the number of words found.<br>1 - Returns an array containing the words in the string.<br>2 - Returns an array where the keys are the positions of the words in the string and the values are the actual words.
char Optional. Specifies special characters to be considered as words.

Technical Details

Return Value: Returns a number or an array, depending on the selected return parameter.
PHP Version: 4.3.0+
--- ---
Changelog: The char parameter was added in PHP 5.1.
--- ---

More Examples

Example 1

Return an array containing the words in the string:

Example 2

Return an array where the keys are the positions of the words in the string and the values are the actual words:

Example 3

Without the char parameter and with the char parameter:


❮ Php Intval Function Php Ref Mysqli ❯