Master PHP’s str_word_count: Count Words, Get Arrays, and Customize Ignored Characters

This guide introduces PHP’s str_word_count() function, explains its syntax and parameters, and provides five practical code examples showing how to count words, retrieve word arrays, obtain word positions, customize ignored characters, and use regular expressions for word matching.

php Courses
php Courses
php Courses
Master PHP’s str_word_count: Count Words, Get Arrays, and Customize Ignored Characters

In PHP, the str_word_count() function counts the number of words in a string. This article explains its usage and provides code examples.

Function Syntax

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

Parameter Description

$string

: required, the string to be examined. $format: optional, determines the return format; 0 (default) returns the word count, 1 returns an array of words, 2 returns an associative array of positions => words. $charlist: optional, a list of characters to ignore when counting words; defaults to whitespace, tabs, newlines, etc.

Usage Examples

Below are several code snippets illustrating how to use str_word_count().

Example 1: Count words in a string

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

Output: Word count: 5

Example 2: Return each word as an array

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

Output: Word list: Hello how are you today

Example 3: Return positions and words

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

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

Example 4: Custom ignore character list

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

Output: Word count: 3

Example 5: Use regular expression to match words

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

Output: Word list: Hello how are you today

The str_word_count() function is a simple yet powerful tool for word counting and extraction in PHP, offering flexible return formats and customizable ignored characters, making it valuable for various string‑processing tasks.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPCode ExamplesString 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

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.