Count how often the word occurs in the text in PHP

Count how often the word occurs in the text in PHP

To count how often a specific word occurs in a text using PHP, you can use several methods. Here are some common approaches:

1. Using substr_count Function

The substr_count function counts the number of times a substring (word) appears in a string. It's straightforward and effective for this purpose.

Example Code:

<?php function countWordOccurrences($text, $word) { // Convert both text and word to lowercase to make the search case-insensitive $text = strtolower($text); $word = strtolower($word); // Count occurrences return substr_count($text, $word); } $text = "This is a test. This test is only a test."; $word = "test"; echo "The word '$word' occurs " . countWordOccurrences($text, $word) . " times."; ?> 

2. Using Regular Expressions

For more flexibility, such as counting whole words or case-insensitive searches, you can use regular expressions with the preg_match_all function.

Example Code:

<?php function countWordOccurrences($text, $word) { // Convert both text and word to lowercase to make the search case-insensitive $text = strtolower($text); $word = strtolower($word); // Use regular expression to match the whole word (case-insensitive) $pattern = '/\b' . preg_quote($word, '/') . '\b/'; preg_match_all($pattern, $text, $matches); // Return the count of matches return count($matches[0]); } $text = "This is a test. This test is only a test."; $word = "test"; echo "The word '$word' occurs " . countWordOccurrences($text, $word) . " times."; ?> 

3. Using str_word_count for Whole Words

If you want to count whole words and not substrings, you can use str_word_count in conjunction with array functions.

Example Code:

<?php function countWordOccurrences($text, $word) { // Convert both text and word to lowercase to make the search case-insensitive $text = strtolower($text); $word = strtolower($word); // Get an array of words $words = str_word_count($text, 1); // Count occurrences return array_count_values($words)[$word] ?? 0; } $text = "This is a test. This test is only a test."; $word = "test"; echo "The word '$word' occurs " . countWordOccurrences($text, $word) . " times."; ?> 

Summary

  • substr_count: Simple and effective for counting substrings.
  • Regular Expressions: Offers flexibility for whole words and case-insensitive searches.
  • str_word_count: Useful for counting whole words with array_count_values.

Choose the method that best fits your needs based on the text structure and the specifics of your word counting requirements.

Examples

  1. How to count word occurrences in a string using substr_count() in PHP?

    Description: Use substr_count() to count how many times a specific substring (word) appears in a string.

    Code:

    <?php $text = "hello world, hello universe"; $word = "hello"; $count = substr_count($text, $word); echo "The word '$word' appears $count times."; ?> 

    This method is case-sensitive.

  2. How to count word occurrences in a string using str_word_count() in PHP?

    Description: Use str_word_count() to count the occurrences of words in a string and then iterate to count specific word occurrences.

    Code:

    <?php $text = "hello world, hello universe"; $words = str_word_count($text, 1); $word = "hello"; $count = array_count_values($words)[$word] ?? 0; echo "The word '$word' appears $count times."; ?> 

    This method considers words based on the default word splitting by spaces and punctuation.

  3. How to count word occurrences using preg_match_all() in PHP?

    Description: Use regular expressions with preg_match_all() to count occurrences of a word.

    Code:

    <?php $text = "hello world, hello universe"; $word = "hello"; $pattern = "/\b" . preg_quote($word, '/') . "\b/i"; preg_match_all($pattern, $text, $matches); $count = count($matches[0]); echo "The word '$word' appears $count times."; ?> 

    This method is case-insensitive and uses word boundaries.

  4. How to count word occurrences with case-insensitivity in PHP?

    Description: Use str_ireplace() to replace the word and count the occurrences.

    Code:

    <?php $text = "Hello world, hello universe"; $word = "hello"; $count = (substr_count(strtolower($text), strtolower($word))); echo "The word '$word' appears $count times."; ?> 

    This method makes both the text and word lowercase for a case-insensitive comparison.

  5. How to count word occurrences in an array of texts in PHP?

    Description: Iterate over an array of strings to count occurrences of a word in all texts.

    Code:

    <?php $texts = ["hello world", "hello universe", "hello everyone"]; $word = "hello"; $count = 0; foreach ($texts as $text) { $count += substr_count($text, $word); } echo "The word '$word' appears $count times."; ?> 
  6. How to count occurrences of a word with regular expressions in PHP?

    Description: Use preg_match_all() with a regex pattern to count the number of occurrences.

    Code:

    <?php $text = "hello world, hello universe, hello galaxy"; $word = "hello"; $pattern = "/\b" . preg_quote($word, '/') . "\b/"; preg_match_all($pattern, $text, $matches); $count = count($matches[0]); echo "The word '$word' appears $count times."; ?> 

    This method counts only whole word occurrences.

  7. How to count occurrences of a word using array_filter() in PHP?

    Description: Split the text into words and use array_filter() to count specific words.

    Code:

    <?php $text = "hello world, hello universe, hello"; $word = "hello"; $words = array_filter(explode(' ', strtolower($text)), function($w) use ($word) { return $w === strtolower($word); }); $count = count($words); echo "The word '$word' appears $count times."; ?> 

    This method converts the text and word to lowercase for a case-insensitive comparison.

  8. How to count word occurrences in a multi-line string in PHP?

    Description: Use preg_match_all() to count occurrences in a multi-line string.

    Code:

    <?php $text = "hello world\nhello universe\nhello galaxy"; $word = "hello"; $pattern = "/\b" . preg_quote($word, '/') . "\b/m"; preg_match_all($pattern, $text, $matches); $count = count($matches[0]); echo "The word '$word' appears $count times."; ?> 
  9. How to count occurrences of a word and ignore punctuation in PHP?

    Description: Remove punctuation from the text and then count the occurrences of a word.

    Code:

    <?php $text = "hello, world! hello universe..."; $word = "hello"; $text = preg_replace("/[^\w\s]/", "", $text); // Remove punctuation $count = substr_count($text, $word); echo "The word '$word' appears $count times."; ?> 
  10. How to count occurrences of a word in a text file in PHP?

    Description: Read the content of a text file and count occurrences of a word.

    Code:

    <?php $filename = 'textfile.txt'; $text = file_get_contents($filename); $word = "hello"; $count = substr_count($text, $word); echo "The word '$word' appears $count times in the file."; ?> 

    Ensure that the file textfile.txt exists and contains the text to be searched.


More Tags

sqldf mode promise run-script appcompatactivity caliburn.micro typescript-typings virtual-keyboard angularjs-directive sax

More Programming Questions

More Math Calculators

More Transportation Calculators

More Fitness Calculators

More Date and Time Calculators