Open In App

How to count the number of words in a string in PHP ?

Last Updated : 04 Jul, 2024
Suggest changes
Share
Like Article
Like
Report

Given a string containing some words and the task is to count number of words in a string str in PHP. In order to do this task, we have the following approaches:

Using str_word_count() Method

The str_word_count() method counts the number of words in a string.

Syntax:

str_word_count(string, return, char)

Example:

PHP
<?php // PHP program to count number of // words in a string  $str = " Geeks for Geeks "; // Using str_word_count() function to // count number of words in a string $len = str_word_count($str); // Printing the result echo $len; ?> 

Output
3

Using trim(), preg_replace(), count() and explode() method. 

Step 1: Remove the trailing and leading white spaces using the trim() method and remove the multiple whitespace into a single space using preg_replace() method.

Step 2: Convert the string into an array using the explode() method.

Step 3: Now count() method counts the number of elements in an array.

Step 4: Resultant is the number of words in a string.

Example:

PHP
<?php // PHP program to count number // of words in a string  // Function to count the words function get_num_of_words($string) { $string = preg_replace('/\s+/', ' ', trim($string)); $words = explode(" ", $string); return count($words); } $str = " Geeks for Geeks "; // Function call  $len = get_num_of_words($str); // Printing the result echo $len; ?> 

Output
3

Using trim(), substr_count(), and str_replace() method. 

Step 1: Remove the trailing and leading white spaces using the trim() method.

Step 2: Convert the multiple white spaces into single space using the substr_count() and str_replace() method.

Step 3: Now counts the number of word in a string using substr_count($str, " ")+1 and return the result.

Example:

PHP
<?php // PHP program to count number // of word in a string  // Function to count the words function get_num_of_words($string) { $str = trim($string); while (substr_count($str, " ") > 0) { $str = str_replace(" ", " ", $str); } return substr_count($str, " ")+1; } $str = " Geeks for Geeks "; // Function call  $len = get_num_of_words($str); // Printing the result echo $len; ?> 

Output
3

Using strtok()

You can count the number of words in a string using strtok() in PHP. Tokenize the string using spaces as delimiters, incrementing a counter for each token until no more tokens are left, effectively counting the words.

Example: In this example use strtok() to tokenize the string by spaces and count each token, effectively counting the number of words in the string. The output of the provided example string " Geeks for Geeks " will be 3.

PHP
<?php function get_num_of_words($string) { // Trim leading and trailing whitespace $str = trim($string); // Initialize word count $wordCount = 0; // Tokenize the string by spaces $token = strtok($str, ' '); // Loop through each token while ($token !== false) { // Increment word count for each token $wordCount++; // Move to the next token $token = strtok(' '); } return $wordCount; } $str = " Geeks for Geeks "; // Function call  $len = get_num_of_words($str); // Printing the result echo $len; ?> 

Output
3

Using regular expressions (preg_match_all()

Using `preg_match_all()` in PHP with the pattern `/(\w+)/u` efficiently counts words by matching sequences of word characters (`\w+`). It returns the number of matches found in the string, providing a robust solution for word counting tasks.

Example

PHP
<?php $string = "Hello World"; $wordCount = preg_match_all('/(\w+)/u', $string, $matches); fwrite(STDOUT, $wordCount . PHP_EOL); ?> 

Output
2 

Using explode() and array_filter()

Another approach to count the number of words in a string in PHP involves using explode() along with array_filter() function. This method splits the string into an array of words based on spaces, filters out empty elements, and counts the remaining elements.

Example:

PHP
<?php $string = " Hello World "; // Step 1: Remove leading and trailing white spaces $string = trim($string); // Step 2: Split the string into an array of words $wordsArray = explode(" ", $string); // Step 3: Filter out empty elements $wordsArray = array_filter($wordsArray); // Step 4: Count the number of words $numWords = count($wordsArray); echo $numWords; // Output: 2 (for "Hello" and "World") ?> 

Output
2



Next Article

Similar Reads