Master PHP’s str_word_count(): Count Words, Get Arrays, and Customize Ignored Characters
This guide explains PHP’s str_word_count() function, covering its syntax, parameters, return formats, and provides multiple code examples demonstrating word counting, array output, position mapping, custom character exclusion, and regex alternatives for effective string processing.
Overview
In PHP, the str_word_count() function counts the number of words in a string. This article details its syntax, parameters, return formats, and provides practical code examples.
Function Syntax
str_word_count(string $string [, int $format = 0 [, string $charlist]])Parameter Description:
$string: required, the string to be analyzed.
$format: optional, determines the return type (0, 1, or 2). Default is 0.
$charlist: optional, a list of characters to ignore during counting. By default, whitespace, tabs, and newlines are ignored.
If $format is 0, the function returns the total word count.
If $format is 1, it returns an array containing each word.
If $format is 2, it returns an associative array where keys are the positions of words and values are the words themselves.
Usage Examples
Below are several examples illustrating how to use str_word_count() in different scenarios.
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);
echo "Word list: ";
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 ignored characters
$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
Conclusion
The str_word_count() function is a simple yet powerful tool in PHP for counting words, extracting word lists, and locating word positions. By adjusting its parameters, developers can tailor its behavior, ignore specific characters, or combine it with regular expressions for flexible string processing.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
