Instance
Count the number of words in the string "Hello world!":
<?phpecho str_word_count ("Hello world!");? >
Definition and usage
The Str_word_count () function calculates the number of words in a string.
Grammar
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:
-
0-Default. Returns the number of words found.
-
1-Returns an array containing the words in the string.
-
2-Returns an array where the key name is the position of the word in the string, and the key value is the actual word.
|
char |
optional. Specifies a special character that is identified as a word. |
technical details
return value: |
Returns a number or an array, depending on the return parameter selected. |
php version: |
4.3.0+ |
update log: |
in PHP 5.1, a new char parameter was added. |
More examples
Example 1
Returns an array containing the words in the string:
<?phpprint_r (Str_word_count ("Hello world!", 1));? >
Example 2
Returns an array where the key name is the position of the word in the string, and the key value is the actual word:
<?phpprint_r (Str_word_count ("Hello world!", 2));? >
Example 3
There are no char parameters and there are char parameters:
<?phpprint_r (Str_word_count ("Hello World & Good morning!", 1));p Rint_r ( Str_word_count ("Hello World & Good morning!", 1, "&"));