定义和用法

str_word_count() 函数计算字符串中的单词数。

语法

str_word_count(string,return,char)
参数描述
string必需。规定要检查的字符串。
return可选。规定 str_word_count() 函数的返回值。
可能的值:
0 - 默认。返回找到的单词的数目。
1 - 返回包含字符串中的单词的数组。
2 - 返回一个数组,其中的键名是单词在字符串中的位置,键值是实际的单词。
char可选。规定被视为单词的特殊字符。

技术细节

返回值:返回数字或者数组,取决于所选择的 return 参数。
PHP 版本:4.3.0+
更新日志:在 PHP 5.1 中,新增了 char 参数。

demo

$str = "this is Jerry & my best frend is tom !";
show(str_word_count($str));
show(str_word_count($str,1));
show(str_word_count($str,2));
show(str_word_count($str,2,'&'));

##输出
8
Array
(
    [0] => this
    [1] => is
    [2] => Jerry
    [3] => my
    [4] => best
    [5] => frend
    [6] => is
    [7] => tom
)
Array
(
    [0] => this
    [5] => is
    [8] => Jerry
    [16] => my
    [19] => best
    [24] => frend
    [30] => is
    [33] => tom
)
Array
(
    [0] => this
    [5] => is
    [8] => Jerry
    [14] => &
    [16] => my
    [19] => best
    [24] => frend
    [30] => is
    [33] => tom
)