str_word_count
- (PHP 4 >= 4.3.0, PHP 5, PHP 7)
- str_word_count — Return information about words used in a string
- str_word_count — 返回字符串中單詞的使用狀況
Description
mixed str_word_count (
string $string [,
int $format = 0 [,
string $charlist ]]
)
/**
Counts the number of words inside string. If the optional format is not specified, then the return value will be an integer representing the number of words found. In the event the format is specified, the return value will be an array, content of which is dependent on the format. The possible value for the format and the resultant outputs are listed below.
統計 string 中單詞的數量。若是可選的參數 format 沒有被指定,那麼返回值是一個表明單詞數量的整型數。若是指定了 format 參數,返回值將是一個數組,數組的內容則取決於 format 參數。format 的可能值和相應的輸出結果以下所列。
For the purpose of this function, 'word' is defined as a locale dependent string containing alphabetic characters, which also may contain, but not start with "'" and "-" characters.
對於這個函數的目的來講,單詞的定義是一個與區域設置相關的字符串。這個字符串能夠包含字母字符,也能夠包含 "'" 和 "-" 字符(但不能以這兩個字符開始)。
*/
Parameters
string
format
- Specify the return value of this function. The current supported values are:
- 指定函數的返回值。當前支持的值以下:
- 0 - returns the number of words found 返回單詞數量
- 1 - returns an array containing all the words found inside the string 返回一個包含 string 中所有單詞的數組
- 2 - returns an associative array, where the key is the numeric position of the word inside the string and the value is the actual word itself 返回關聯數組。數組的鍵是單詞在 string 中出現的數值位置,數組的值是這個單詞
charlist
- A list of additional characters which will be considered as 'word'
- 附加的字符串列表,其中的字符將被視爲單詞的一部分。
Return Values
- Returns an array or an integer, depending on the format chosen.
- 返回一個數組或整型數,這取決於 format 參數的選擇。
Changelog
- 5.1.0 Added the charlist parameter
Examples
<?php
/**
* Created by PhpStorm.
* User: zhangrongxiang
* Date: 2018/3/8
* Time: 下午9:48
*/
$str = "Hello fri3nd, you're
looking good today!";
//[0] => Hello
//[1] => fri
//[2] => nd
//[3] => you're
//[4] => looking
//[5] => good
//[6] => today
print_r( str_word_count( $str, 1 ) );
//[0] => Hello
//[6] => fri
//[10] => nd
//[14] => you're
//[28] => looking
//[45] => good
//[50] => today
print_r( str_word_count( $str, 2 ) );
//[0] => Hello
//[1] => fri3nd
//[2] => you're
//[3] => looking
//[4] => good
//[5] => today
print_r( str_word_count( $str, 1, 'àáãç3' ) );
//[0] => Hello
//[1] => fri3nd
//[2] => you're
//[3] => looking
//[4] => good
//[5] => today
//[6] => look123
//[7] => ing
$str = "Hello fri3nd, you're
looking good today!
look1234ing";
print_r( str_word_count( $str, 1, '0..3' ) );
See
All rights reserved