PHP str_word_count () function
Examples
Calculate the number of words the string "Hello World!" In:
<?php
echo str_word_count("Hello world!");
?>
echo str_word_count("Hello world!");
?>
Running instance »
Definition and Usage
str_word_count () function calculates the number of words in the string.
grammar
str_word_count( string,return,char )
参数 | 描述 |
---|---|
string | 必需。规定要检查的字符串。 |
return | 可选。规定 str_word_count() 函数的返回值。 可能的值:
|
char | 可选。规定被认定为单词的特殊字符。 |
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, add the char argument. |
More examples
Example 1
It returns an array containing a string of words:
<?php
print_r(str_word_count("Hello world!",1));
?>
print_r(str_word_count("Hello world!",1));
?>
Running instance »
Example 2
Returns an array of keys is the word position in the string, the key is the actual word:
<?php
print_r(str_word_count("Hello world!",2));
?>
print_r(str_word_count("Hello world!",2));
?>
Running instance »
Example 3
There is no char char parameters and parameters:
<?php
print_r(str_word_count("Hello world & good morning!",1));
print_r(str_word_count("Hello world & good morning!",1,"&"));
?>
print_r(str_word_count("Hello world & good morning!",1));
print_r(str_word_count("Hello world & good morning!",1,"&"));
?>
Running instance »
PHP String Reference