str_word_count()

Description

This function counts the number of words in a string. It can optionally return the word count or a list of the words found as an array. You can customize the list of characters that the function considers valid for a word.

Syntax


mixed str_word_count( string $str [, int $format = 0 [, string $charlist ]] )

Parameters

$str
The string whose words are to be counted.
$format
A flag to set the type of output returned by the function as follow:

  • 0 – returns the number of words found in $str.
  • 1 – returns an array of the words found in $str.
  • 2 – returns an associative array of the words found in $str where the key is the character position of word and the value is the word itself.
$charlist
A list of additional characters which will be considered part of a word.

Return Value

mixed
One of the following types is returned depending upon the value of the $format parameter:

  • int – if $format is passed as 0 (the default)
  • array of strings – if the $format is passed as 1
  • associative array of strings – if the $format is passed as 2, where the key is the character position of the word in $str and the value is the word itself

Examples

1. Get the Word Count

This example passes just the $str variable to str_to_word(), causing it to return a count of the number of words found.

<?php
$str = "How now, brown cow?";
echo "Word count = " . str_word_count($str) . "\n";
?>

The str_to_word() function returned the value 4, the number of words in the passed $str variable.

Word count = 4

2. Get the Words in an Array

This example passes 1 for the $format, causing str_to_word() to return an array of the words it found. The print_r() function is called to display the array’s indexes and values.

<?php
$str = "How now, brown cow?";
print_r( str_word_count( $str, 1 ));
?>

The returned array has four elements, each corresponding to a word in the passed $str variable. Note that the comma (,) and question mark (?) are not included in the words found as they are not in the default list of characters allowed in words. The default list includes only the apostrophe (‘) and the hyphen (-).

Array
(
    [0] => How
    [1] => now
    [2] => brown
    [3] => cow
)

3. Example heading

In this example, a 2 is passed for the $format parameter, causing str_to_word() to return an associative array of the words it found. The print_r() function is called to display the array’s keys and values.

<?php
$str = "How now, brown cow?";
print_r( str_word_count( $str, 2 ));
?>

The output is shown below. As in the previous example, the returned array has four elements, each corresponding to a word in the passed $str variable. The array keys correspond to the character positions of the first character of each word. The array values are the words themselves and are the same as the previous example. Note that the positions begin with 0 and include the embedded punctuation from the source string even though the words themselves do not include punctuation.

(
    [0] => How
    [4] => now
    [9] => brown
    [15] => cow
)

4. Alter the Character List

In this example the $str variable contains a word with an apostrophe and embedded dashes. It also has a word with an embedded number. The $str variable is passed to str_word_count() twice. In both calls, an associative array format is requested. In the second call, the zero character (0) is added to the list of characters that can make up a word.

<?php
$str = "You're not up-to-date on your w0rk.";
print_r( str_word_count( $str, 2 ));
print_r( str_word_count( $str, 2, "0" ));
?>

The output is shown below. In the first call to str_word_count(), the default character list includes the apostrophe and the hyphen so “You’re” and “up-to-date” are identified as words. However, the word “w0rk” is broken on the embedded zero character (0) into two words.

In the second call to str_word_count(), the zero character is added to the list of allowable word-characters so “w0rk” is identified as a valid word.

Array
(
    [0] => You're
    [7] => not
    [11] => up-to-date
    [22] => on
    [25] => your
    [30] => w
    [32] => rk
)
Array
(
    [0] => You're
    [7] => not
    [11] => up-to-date
    [22] => on
    [25] => your
    [30] => w0rk
)

See Also

References