Latest web development tutorials

PHP str_word_count () function

PHP String Reference PHP String Reference

Examples

Calculate the number of words the string "Hello World!" In:

<?php
echo str_word_count("Hello world!");
?>

Running instance »

Definition and Usage

str_word_count () function calculates the number of words in the string.


grammar

str_word_count( string,return,char )

参数 描述
string 必需。规定要检查的字符串。
return 可选。规定 str_word_count() 函数的返回值。

可能的值:

  • 0 - 默认。返回找到的单词的数目。
  • 1 - 返回包含字符串中的单词的数组。
  • 2 - 返回一个数组,其中的键名是单词在字符串中的位置,键值是实际的单词。
char 可选。规定被认定为单词的特殊字符。

technical details

return value: Returns a number or an array, depending on the return parameter selected.
PHP version: 4.3.0+
Update log: In PHP 5.1, add the char argument.


More examples

Example 1

It returns an array containing a string of words:

<?php
print_r(str_word_count("Hello world!",1));
?>

Running instance »

Example 2

Returns an array of keys is the word position in the string, the key is the actual word:

<?php
print_r(str_word_count("Hello world!",2));
?>

Running instance »

Example 3

There is no char char parameters and parameters:

<?php
print_r(str_word_count("Hello world & good morning!",1));
print_r(str_word_count("Hello world & good morning!",1,"&"));
?>

Running instance »


PHP String Reference PHP String Reference