Backend Development 4 min read

Using PHP str_word_count() Function to Count Words in a String

This article explains the PHP str_word_count() function, detailing its syntax, parameters, and return formats, and provides multiple code examples demonstrating how to count words, retrieve word lists, obtain word positions, and customize ignored characters for string processing.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP str_word_count() Function to Count Words in a String

In PHP, the str_word_count() function is used to count the number of words in a string. This article provides a detailed guide on its usage with code examples.

Function Syntax

<code>str_word_count(string $string [, int $format = 0 [, string $charlist]])</code>

Parameter Description

$string : Required. The string whose words are to be counted.

$format : Optional. Determines the return format; 0 (default) returns the word count, 1 returns an array of words, 2 returns an associative array with word positions as keys.

$charlist : Optional. A list of characters to ignore when counting words; defaults to whitespace, tabs, and line breaks.

Usage Examples

Example 1: Count the number of words in a string

<code>$string = "Hello, how are you today?";
$wordCount = str_word_count($string);
echo "Word count: " . $wordCount;
</code>

Output: Word count: 5

Example 2: Return each word as an array

<code>$string = "Hello, how are you today?";
$wordsArray = str_word_count($string, 1);
echo "Word list: ";
foreach ($wordsArray as $word) {
    echo $word . " ";
}
</code>

Output: Word list: Hello how are you today

Example 3: Return word positions and words

<code>$string = "Hello, how are you today?";
$wordsArray = str_word_count($string, 2);
echo "Word list: ";
foreach ($wordsArray as $position => $word) {
    echo "Position" . $position . ":" . $word . " ";
}
</code>

Output: Word list: Position0:Hello Position6:how Position10:are Position14:you Position18:today

Example 4: Custom ignore character list

<code>$string = "Hello, how are you today?";
$wordCount = str_word_count($string, 0, "o");
echo "Word count: " . $wordCount;
</code>

Output: Word count: 3

Example 5: Use regular expression to match words

<code>$string = "Hello, how are you today?";
preg_match_all('/\w+/', $string, $matches);
echo "Word list: ";
foreach ($matches[0] as $word) {
    echo $word . " ";
}
</code>

Output: Word list: Hello how are you today

The str_word_count() function is a simple yet powerful tool for word counting, word list extraction, and locating word positions in PHP string processing, and it can be customized by specifying characters to ignore.

Backend DevelopmentPHPstring processingstr_word_countword count
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.