English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
PHP String String Functions Manual
The str_word_count() function is used to calculate the number of words in a string.
mixed str_word_count(string $string[, int $format = 0[, string $charlist]])
Counts the number of words in the string. If the optional parameter format is not specified, the return value is an integer representing the number of words. If the format parameter is specified, the return value will be an array, the content of which depends on the format parameter. The possible values and corresponding output results are listed as follows.
For the purpose of this function, the definition of a word is a string related to the locale. This string can contain letter characters, as well as "'" and ".-" character (but cannot start with these two characters).
PHP 5.1.0 Version Added charlist Parameter.
Returns an array or an integer depending on the choice of the format parameter.
Serial Number | Parameters and Description |
---|---|
1 | string Required. Specify the string to be checked. |
2 | format Optional. Specify the return value of the str_word_count() function.Possible values:
|
3 | charlist Optional. An additional string list, where the characters are considered as part of the word. |
Try the following example, which returns an array containing the words in the string and calculates the number of words in the string:
<?php //Calculates the number of words in a string. echo str_word_count("w3codebox simply easy learning"); //Returns an array containing the words in the string print_r(str_word_count("Can i help you!",1)); //without charlist parameter print_r(str_word_count("Can i help you & what's your name!",1)); //with charlist parameter print_r(str_word_count("Can i help you & what's your name!",1'; ?>Test and see‹/›
Output Result
4 Array ( [0] => Can [1=> i [2=> help [3=> you ) Array ( [0] => Can [1=> i [2=> help [3=> you [4=> what's [5=> your [6=> name ) Array ( [0] => Can [1=> i [2=> help [3=> you [4=> & [5=> what's [6=> your [7=> name )