The str_word_count()
function returns the number of words in a string.
PHP str_word_count() Function has the following syntax.
mixed str_word_count ( string str [, int count_type [, string char_list]] )
Parameter | Description |
---|---|
string | The string |
format | Specify the return value of this function. |
charlist | A list of additional characters which will be considered as 'word' |
The current supported values for format are:
PHP str_word_count() Function returns an array or an integer, depending on the format chosen.
Here are examples of the three options:
<?PHP// ww w . j av a 2 s. com
$str = "This is a test from java2s.com.";
$a = str_word_count($str, 1);
print_r($a);
print "\n";
$b = str_word_count($str, 2);
print_r($b);
print "\n";
$c = str_word_count($str);
print_r($c);
echo "There are $c words in the string\n";
?>
The code above generates the following result.