PHP: 3 Ways to count the number of words in a string

Updated: January 9, 2024 By: Guest Contributor Post a comment

Counting words in a string is a common task in PHP development, useful in scenarios such as generating word counts for documents, form input validation, or even resource allocation based on textual analysis. There are numerous methods to accomplish this, each with their own set of advantages and limitations. In this article, we will explore several PHP functions and techniques to count words within a string.

Solution 1: Using str_word_count

The str_word_count function is a simple and direct way to count words in a string. It provides robust functionality with options to count words, return an array of words, or even an associative array detailing the position of words.

  1. Create a string variable containing the text to analyze.
  2. Call the str_word_count function, pass the string variable, and choose the format parameter accordingly.

Example:

$text = "Hello world! The quick brown fox jumps over the lazy dog.";
$wordCount = str_word_count($text, 0);
echo $wordCount; // Outputs the number of words

Notes: str_word_count is efficient for English text, but may not recognize words with apostrophes or hyphens correctly. It’s also influenced by the locale setting and may not behave as expected with non-Latin characters.

Solution 2: Using preg_match_all

For more complex word counting, particularly where enhanced control over what constitutes a word is required, preg_match_all with an appropriate regular expression can be used. This method can account for words with special characters.

Steps:

  1. Define a string variable containing the target text.
  2. Define the regular expression pattern that constitutes a ‘word’.
  3. Utilize preg_match_all to find all matches of the pattern.
  4. Count the matches array, which contains all of the instances of words in the string.

Example:

$text = "Hello world! That's a fox: Mr. Foxworthy.";
$pattern = '/\b[\w\'\-]+\b/';
$matches = [];
preg_match_all($pattern, $text, $matches);

$wordCount = count($matches[0]);
echo $wordCount; // Outputs the accurate number of words

Notes: This method is powerful and flexible but can be slower than str_word_count because of the overhead of regular expression processing. Defining a correct pattern is also critical, as a poorly designed pattern can yield inaccurate results.

Solution 3: Using explode and array_filter

Another approach to count words is by splitting the string into an array using a whitespace as a delimiter and then filtering out any non-word elements. This can be useful when you have a well-structured text without special characters.

Steps:

  1. Declare a string with the content to be analyzed.
  2. Use explode to split the string into an array of words based on whitespace.
  3. Filter out empty array elements using array_filter.
  4. Count the results.

Example:

$text = "Hello world! This should be eight words.";
$wordsArray = explode(' ', $text);
$wordsArray = array_filter($wordsArray);

$wordCount = count($wordsArray);
echo $wordCount; // Outputs the number of words

Notes: The primary limitation of this solution is its inability to handle complex text with punctuation effectively. It also splits on whitespace only, which may not always represent word boundaries correctly.

Conclusion

In conclusion, the task of counting words within a string can be achieved through several methods in PHP, each serving different needs and use cases. The str_word_count function is straightforward and suitable for simple word counting tasks but may falter with complex or non-standard text. For more advanced word recognition, regex-based methods such as preg_match_all allow for customizable and accurate counts. And for simpler, well-structured texts, exploding the string into an array may suffice. Ultimately, the choice of method should be aligned with the complexity of the text and the performance requirements of the project.