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

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:

Approach 1: Using str_word_count() Method: The str_word_count() method is used to counts the number of words in a string.

Syntax:

str_word_count(string, return, char)

Example:

PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right


Output



3

Approach 2: Here the idea is to use 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

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right


Output

3

Approach 3: Here the idea is to use 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

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right


Output

3



My Personal Notes arrow_drop_up

Small things always make you to think big

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.