(PHP 4 >= 4.3.0, PHP 5)
str_word_count — 返回字符串中單詞的使用情況
說明
mixed str_word_count ( string$string
[, int $format
= 0
[, string $charlist
]] )
統計 string
中單詞的數量。如果可選的參數 format
沒有被指定,那么返回值是一個代表單詞數量的整型數。如果指定了 format
參數,返回值將是一個數組,數組的內容則取決于 format
參數。format
的可能值和相應的輸出結果如下所列。
對于這個函數的目的來說,單詞的定義是一個與區域設置相關的字符串。這個字符串可以包含字母字符,也可以包含 "'" 和 "-" 字符(但不能以這兩個字符開始)。
參數
string
字符串。
format
指定函數的返回值。當前支持的值如下:
0 - 返回單詞數量
1 - 返回一個包含 string
中全部單詞的數組
2 - 返回關聯數組。數組的鍵是單詞在 string
中出現的數值位置,數組的值是這個單詞
charlist
附加的字符串列表,其中的字符將被視為單詞的一部分。
返回值
返回一個數組或整型數,這取決于 format
參數的選擇。
更新日志
版本
說明
5.1.0
新增 charlist
參數。
范例
Example #1 str_word_count() 范例
<?php
$str = "Hello fri3nd, you're
looking good today!";
print_r(str_word_count($str, 1));
print_r(str_word_count($str, 2));
print_r(str_word_count($str, 1, 'àá??3'));
echo str_word_count($str);
?>
以上例程會輸出:
Array ( [0] => Hello [1] => fri [2] => nd [3] => you're [4] => looking [5] => good [6] => today ) Array ( [0] => Hello [6] => fri [10] => nd [14] => you're [29] => looking [46] => good [51] => today ) Array ( [0] => Hello [1] => fri3nd [2] => you're [3] => looking [4] => good [5] => today ) 7
參見
explode() - 使用一個字符串分割另一個字符串 preg_split() - 通過一個正則表達式分隔字符串 split() - 用正則表達式將字符串分割到數組中 count_chars() - 返回字符串所用字符的信息 substr_count() - 計算字串出現的次數