PHP substr_count() Function

PHP

PHP substr_count() - Count Substring Occurrences

SEO Description: Learn PHP substr_count() function. Count the number of occurrences of a substring.

The substr_count() function in PHP is a powerful and straightforward tool to count the number of times a substring appears within a larger string. It is essential when analyzing text data, searching for specific tokens, or simply verifying the frequency of certain patterns in text strings.

Prerequisites

  • Basic knowledge of PHP programming.
  • Understanding of strings and substrings in PHP.
  • A working PHP development environment (PHP 5 or higher recommended).

Setup Steps

  1. Ensure you have PHP installed on your system. You can verify by running php -v in your terminal.
  2. Create a PHP file, e.g., substr_count_example.php.
  3. Open the file in your preferred code editor.
  4. Use the code examples below to apply substr_count() in your scripts.

What is substr_count()?

The substr_count() function counts the number of times a substring occurs in a string. Its syntax is:

int substr_count ( string $haystack , string $needle [, int $offset = 0 [, int $length ]] )
  • $haystack: The full string to search in.
  • $needle: The substring to search for.
  • $offset: (optional) The starting position in $haystack to begin searching. Defaults to 0.
  • $length: (optional) The maximum length from $offset to search within the string.

Examples Explained

Example 1: Basic Usage

<?php
$text = "PHP is a popular general-purpose scripting language that is especially suited to web development.";
$substring = "is";

$count = substr_count($text, $substring);
echo "The substring '$substring' appears $count times.";
?>

Output: The substring 'is' appears 2 times.

Explanation: This example counts how many times "is" appears in the given sentence.

Example 2: Using Offset and Length

<?php
$text = "Hello world! Welcome to the world of PHP programming.";
$substring = "world";

// Count occurrences starting from position 13 (skips the first "world")
$count = substr_count($text, $substring, 13);
echo "Occurrences of '$substring' from offset 13: $count";
?>

Output: Occurrences of 'world' from offset 13: 1

Explanation: The first "world" is before position 13, so only subsequent occurrences are counted.

Example 3: Case Sensitivity

<?php
$text = "Apple apples APPLES aPpLe";
$substring = "apple";

$count = substr_count($text, $substring);
echo "Occurrences of '$substring': $count";
?>

Output: Occurrences of 'apple': 0

Explanation: substr_count() is case-sensitive, so uppercase or mixed-case does not match lowercase "apple". To count case-insensitive occurrences, use str_ireplace() or relevant functions.

Best Practices

  • Remember substr_count() is case-sensitive; convert strings to one case if necessary with strtolower() or strtoupper().
  • Use optional $offset and $length parameters to search within specific parts of a string.
  • Validate that $needle is not an empty string; an empty needle will cause a warning.
  • For binary-safe counting or multibyte strings, consider encoding or alternative functions if needed.

Common Mistakes

  • Passing an empty string as $needle (will trigger a warning).
  • Using substr_count() expecting case-insensitive matches without preprocessing.
  • Confusing substr_count() with strpos(); the former counts all occurrences, the latter returns the first position.
  • Not considering overlapping substrings, e.g., counting "ana" in "banana" only counts non-overlapping occurrences.

Interview Questions

Junior-Level

  • Q1: What does PHP's substr_count() function do?
    A: It counts the number of times a substring appears in a string.
  • Q2: Is substr_count() case-sensitive?
    A: Yes, it is case-sensitive.
  • Q3: What parameters does substr_count() accept?
    A: The main string ($haystack), the substring ($needle), and optional $offset and $length.
  • Q4: What happens if you pass an empty string as the substring?
    A: It triggers a warning and the function returns false or 0 depending on context.
  • Q5: Can substr_count() count overlapping substrings?
    A: No, it counts only non-overlapping occurrences.

Mid-Level

  • Q1: How would you count case-insensitive occurrences of a substring in PHP?
    A: Convert both strings to lowercase using strtolower() before using substr_count().
  • Q2: How can you limit substr_count() to only count within a part of the string?
    A: Use the optional $offset and $length parameters.
  • Q3: What is the behavior of substr_count() regarding multibyte character strings?
    A: It is not multibyte-safe; results may be unreliable with multibyte encodings.
  • Q4: How would you handle counting a substring like "ana" in "banana" if you wanted overlapping counts?
    A: substr_count() doesn't count overlaps; you'd need a custom loop or regex with preg_match_all().
  • Q5: What types of variable should substr_count() parameters be?
    A: Both $haystack and $needle should be strings.

Senior-Level

  • Q1: How would you improve performance when counting many substrings in a large string?
    A: Use a single pass parsing algorithm or regex matching all substrings at once instead of multiple substr_count() calls.
  • Q2: Can substr_count() be used reliably for binary data?
    A: It works with binary-safe strings but beware of multibyte characters and encoding issues.
  • Q3: How would you count substrings in multibyte encoded strings (e.g. UTF-8)?
    A: Use multibyte string functions or regular expressions that support UTF-8, as substr_count() is not multibyte-aware.
  • Q4: Describe an approach to count overlapping substring occurrences efficiently in PHP.
    A: Implement a sliding window algorithm or use preg_match_all() with positive lookahead regex to count overlaps.
  • Q5: How would you handle substring counting in a case where the substring could be a regex special character?
    A: Escape the substring before using regex functions or avoid regex by using substr_count() on preprocessed strings.

Frequently Asked Questions (FAQ)

  • Q: What happens if the $needle is empty?
    A: PHP will emit a warning and the function will return false or 0 depending on context. Always ensure the $needle is not empty.
  • Q: Can substr_count() count overlapping substrings?
    A: No, it only counts non-overlapping occurrences.
  • Q: How to make substring counting case-insensitive?
    A: Convert both $haystack and $needle to the same case before using substr_count().
  • Q: Is substr_count() available in all PHP versions?
    A: It has been available since PHP 4, so it’s compatible with nearly all PHP versions.
  • Q: What is the difference between substr_count() and strpos()?
    A: substr_count() counts how many times a substring appears; strpos() returns the position of the first occurrence.

Conclusion

The substr_count() function is a simple yet effective way to find out how many times a substring appears within a string in PHP. While it's case-sensitive and doesn't support overlapping matches, with a bit of preprocessing or combination with other string functions, it can be adapted to various programming needs. Understanding its parameters and quirks helps you perform text frequency analysis and develop robust string handling solutions efficiently.