PHP str_word_count() 函數(shù)
PHP String 參考手冊
實(shí)例
計(jì)算字符串 "Hello World!" 中的單詞數(shù):
<?php
echo str_word_count("Hello world!");
?>
運(yùn)行實(shí)例 ?
定義和用法
str_word_count() 函數(shù)計(jì)算字符串中的單詞數(shù)。
語法
str_word_count(string,return,char)
參數(shù) | 描述 |
string | 必需。規(guī)定要檢查的字符串。 |
return | 可選。規(guī)定 str_word_count() 函數(shù)的返回值。 可能的值: - 0 - 默認(rèn)。返回找到的單詞的數(shù)目。
- 1 - 返回包含字符串中的單詞的數(shù)組。
- 2 - 返回一個(gè)數(shù)組,其中的鍵名是單詞在字符串中的位置,鍵值是實(shí)際的單詞。
|
char | 可選。規(guī)定被認(rèn)定為單詞的特殊字符。 |
技術(shù)細(xì)節(jié)
返回值: | 返回一個(gè)數(shù)字或者一個(gè)數(shù)組,取決于所選擇的 return 參數(shù)。 |
PHP 版本: | 4.3.0+ |
更新日志: | 在 PHP 5.1 中,新增了 char 參數(shù)。 |
更多實(shí)例
實(shí)例 1
返回包含字符串中的單詞的數(shù)組:
<?php
print_r(str_word_count("Hello world!",1));
?>
運(yùn)行實(shí)例 ? 實(shí)例 2
返回一個(gè)數(shù)組,其中的鍵名是單詞在字符串中的位置,鍵值是實(shí)際的單詞:
<?php
print_r(str_word_count("Hello world!",2));
?>
運(yùn)行實(shí)例 ? 實(shí)例 3
沒有 char 參數(shù)和有 char 參數(shù):
<?php
print_r(str_word_count("Hello world & good morning!",1));
print_r(str_word_count("Hello world & good morning!",1,"&"));
?>
運(yùn)行實(shí)例 ?
PHP String 參考手冊