Current Location: Home> Latest Articles> Detailed Explanation on How to Use the substr_count Function to Count the Occurrence of a Character in a String

Detailed Explanation on How to Use the substr_count Function to Count the Occurrence of a Character in a String

gitbox 2025-06-15

1. substr_count() Function Basic Syntax

substr_count() function basic syntax is as follows:

substr_count(string $haystack, string $needle, int $offset = 0, int $length = NULL): int
  • $haystack: The main string to search in.

  • $needle: The substring (or single character) to search for.

  • $offset: Optional parameter that specifies the position to start searching from (defaults to the beginning of the string).

  • $length: Optional parameter that specifies the maximum length to search (defaults to searching the entire string).

This function returns an integer indicating how many times $needle appears in $haystack. If $needle is not found in $haystack, it returns 0.


2. Basic Example

Let’s assume we have a string, and we want to count how many times a particular character appears. Here’s a simple example:

<?php
$string = "hello world, hello PHP!";
$char = "o";
<p>// Using substr_count function to count occurrences of character "o"<br>
$count = substr_count($string, $char);</p>
<p>echo "The character '$char' appears $count times."; // Output: The character 'o' appears 3 times.<br>
?><br>

In the above code, we counted how many times the character "o" appears in the string "hello world, hello PHP!", and the result is 3.


3. Counting Substring Occurrences

In addition to counting occurrences of a single character, substr_count() can also be used to count how many times a substring appears in the target string. Here’s an example:

<?php
$string = "abcabcabcabc";
$substring = "abc";
<p>// Using substr_count function to count occurrences of the substring "abc"<br>
$count = substr_count($string, $substring);</p>
<p>echo "The substring '$substring' appears $count times."; // Output: The substring 'abc' appears 4 times.<br>
?><br>

In this example, we counted how many times the substring "abc" appears in the string "abcabcabcabc", and the result is 4.


4. Using offset and length Parameters

The substr_count() function also supports offset and length parameters, allowing us to limit the search range. Here’s an example:

<?php
$string = "hello world, hello PHP!";
$char = "o";
<p>// Start searching from index 5 and limit the search to 10 characters<br>
$count = substr_count($string, $char, 5, 10);</p>
<p>echo "The character '$char' starting from index 5 and searching up to 10 characters appears $count times."; // Output: The character 'o' starting from index 5 and searching up to 10 characters appears 1 time.<br>
?><br>

In this example, we started searching from the 5th character in the string, limiting the search length to 10 characters. Therefore, we only counted occurrences of the character "o" in the substring "world, hello PHP!", which results in 1.


5. Notes

  • The substr_count() function is case-sensitive, so "o" and "O" are treated as different characters. If you want to perform a case-insensitive search, you can first convert the string to a uniform case using strtolower() or strtoupper().

    For example:

    <?php
    $string = "Hello World, hello PHP!";
    $char = "o";
    <p>// Convert the string to lowercase before searching<br>
    $count = substr_count(strtolower($string), strtolower($char));</p>
    <p>echo "The character '$char' appears in a case-insensitive manner $count times."; // Output: The character 'o' appears in a case-insensitive manner 3 times.<br>
    ?><br>
    

  • If $needle is an empty string, substr_count() will return 0. This is because an empty string cannot be counted as a valid substring.