PHP: str_word_count() function
PHP: Return information about words used in a string
The str_word_count() function is used to count the number of words in a string.
Version:
(PHP 4 and above)
Syntax:
str_word_count(string_name, nformat, add_chars)
Parameter:
Name | Description | Required / Optional |
Type |
---|---|---|---|
string_name | The input string. | Required | String |
nformat | Specifies the return value of the str_word_count() function. Supported values : 0 - returns the number of words found. 1 - returns an array contains all the words found within the specified string. 2 - returns an associative array, where the key is the numeric position of the word inside the string and the value is the actual word itself. |
Optional | Integer |
add_chars | A list of additional characters which will be considered as 'word'. | Optional | String |
Return value:
An array or an integer, depending on the format chosen.
Value Type: Mixed.
*Mixed: Mixed indicates that a parameter may accept multiple (but not necessarily all) types.
Pictorial Presentation
Example:
<?php
$string_name='Welcome to w3resource.com';
print_r(str_word_count($string_name,0));
echo '<br>';
print_r(str_word_count($string_name,1));
echo '<br>';
print_r(str_word_count($string_name,2));
?>
Output :
5
Array ( [0] => Welcome [1] => to [2] => w [3] => resource [4] => com )
Array ( [0] => Welcome [8] => to [11] => w [13] => resource [22] => com )
View the example in the browser
See also
Previous: str_split
Next: strcasecmp
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/php/function-reference/str_word_count.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics