VOOZH about

URL: https://www.geeksforgeeks.org/php/how-to-count-the-number-of-words-in-a-string-in-php/

⇱ Count the number of words in a string in PHP - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Count the number of words in a string in PHP

Last Updated : 5 Dec, 2025

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.

  • Fastest and simplest built-in method specifically made for counting words.
  • Reliable for basic ASCII text, requiring almost no preprocessing.

Syntax:

str_word_count(string, return, char)

Example:


Output
3

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

A flexible approach that first cleans the string, then counts words manually.

  • Ideal when input contains inconsistent or excessive whitespace.
  • Gives full control over what should be considered a word.

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:


Output
3

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

A lightweight method that counts spaces after normalizing the string.

  • Efficient for very large text because it avoids regex.
  • Good when you only need to collapse multiple spaces into single spaces.

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:


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.

  • More memory-efficient than splitting the whole string at once.
  • Useful when you need step-by-step control over token handling.

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.


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.

  • Best for Unicode or multilingual text where str_word_count() fails.
  • Allows defining complex word rules, including hyphens or special characters.

Example


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.

  • Helpful when you want to inspect, transform, or filter the word list.
  • Offers full control and avoids PHP’s default word-detection logic.

Example:


Output
2
Comment