Using PHP str_word_count() to Count Words, Retrieve Word Arrays, and Get Word Positions
This article explains the PHP str_word_count() function, showing how to count words in a string, obtain an array of words, and retrieve an associative array of words with their positions, complete with clear code examples and output explanations.
The str_word_count() function is a built‑in PHP function used to count the number of words in a string. By passing a string as the first argument, the function returns the word count, and additional parameters allow it to return word arrays or word‑position maps.
Basic Usage
The simplest usage of str_word_count() requires only a string argument and returns the total number of words. For example:
<code>$text = "Hello world! This is a sample text.";
$count = str_word_count($text);
echo $count; // outputs: 7</code>In this example, $text holds a sentence containing seven words. The function counts these words and stores the result in $count , which is then printed.
Counting Words (second parameter = 0)
By specifying the second parameter as 0 , the function behaves the same as the default, returning only the word count:
<code>$text = "Hello world! This is a sample text.";
$count = 0;
$count = str_word_count($text, 0);
echo $count; // outputs: 7</code>Here $count is initialized to zero, then overwritten with the result of str_word_count() , which again yields seven.
Returning an Array of Words (second parameter = 1)
When the second argument is set to 1 , the function returns an indexed array containing each word found in the string:
<code>$text = "Hello world! This is a sample text.";
$words = str_word_count($text, 1);
print_r($words);</code>The variable $words becomes an array like Array ( [0] => Hello [1] => world [2] => This [3] => is [4] => a [5] => sample [6] => text ) , which is displayed using print_r() .
Returning Words with Their Positions (second parameter = 2)
Setting the second parameter to 2 makes the function return an associative array where the keys are the numeric positions of each word in the original string and the values are the words themselves:
<code>$text = "Hello world! This is a sample text.";
$words = str_word_count($text, 2);
print_r($words);</code>The resulting array looks like Array ( [0] => Array ( [0] => Hello [6] => world [12] => This [17] => is [20] => a [22] => sample [29] => text ) ) , indicating each word’s starting offset.
Summary
The str_word_count() function can be used to count words, retrieve an array of words, or obtain an associative array of words with their positions by passing different values for the optional second argument. Mastering this function enables efficient word‑level processing of strings in PHP.
PHP Learning Recommendations
Vue3+Laravel8+Uniapp Beginner to Practical Development Tutorial
Vue3+TP6+API Social E‑commerce System Development Course
Swoole From Beginner to Expert Recommended Course
Workerman+TP6 Real‑time Chat System Limited‑time Offer
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.