当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > str_word_count() 函数
The str_word_count() function counts the number of words in a string.
str_word_count()函数的作用是:计算字符串中的词数。
str_word_count(string,return,char) |
Parameter参数 | Description描述 |
---|---|
string | Required. Specifies the string to check 必要参数。指定字符串对象 |
return | Optional. Specifies the return value of the str_word_count() function. 可选参数。指定str_word_count()函数需要返回的值。 Possible values:
|
char | Optional. Specifies special characters to be considered as words. 可选参数。指定组成词的特殊字符 Note: This parameter was added in PHP 5.1 |
<?php echo str_word_count("Hello world!"); ?> |
The output of the code above will be:
上述代码将输出下面的结果:
2 |
<?php print_r(str_word_count("Hello world!",1)); ?> |
The output of the code above will be:
上述代码将输出下面的结果:
Array ( [0] => Hello [1] => world ) |
<?php print_r(str_word_count("Hello world!",2)); ?> |
The output of the code above will be:
上述代码将输出下面的结果:
Array ( [0] => Hello [6] => world ) |
str_word_count() without and with the char parameter:
带有 / 不带有char参数的str_word_count()函数举例:
<?php print_r(str_word_count("Hello world & good morning!",1)); print_r(str_word_count("Hello world & good morning!",1,"&")); ?> |
The output of the code above will be:
上述代码将输出下面的结果:
Array ( [0] => Hello [1] => world [2] => good [3] => morning ) Array ( [0] => Hello [1] => world [2] => & [3] => good [4] => morning ) |