Friday 26 August 2016

Count Words in a String in PHP

In Some cases we need to calculate or count the number of words in a sentence or a long string.
This simple function takes a string as parameter and counts the number of words in the string based on the space in the string.

function countWords($string) {
   $word_array = explode(" ", $string);
   return count($word_array);
}

No comments:

Post a Comment