Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP String substr_count() Function



The PHP String substr_count() function is used to count the number of times a substring comes in a given string. The function also allows us to search for the given substring inside the given range of the index.

This function is case sensitive, so the "abc" substring does not appear in the string "Abcab". If the (start+length) value selected for search exceeds the size of the given string, the user gets a warning message.

Syntax

Below is the syntax of the PHP String substr_count() function −

int substr_count ( $string, $substring, $start, $length )

Parameters

Here are the parameters of the substr_count() function −

  • $string − (Required) It is the string to search in.

  • $substring − (Required) It is the substring to search for.

  • $start − (Optional) It indicates where to start counting. If the offset is negative, counting begins at the end of the string.

  • $length − (Optional) It is the maximum length after the given start to search for the substring. It generates a warning if the start plus length exceeds the string length. A negative length starts from the end of the string.

Return Value

The substr_count() function returns number of times the substring appears in the string.

PHP Version

First introduced in core PHP 4, the substr_count() function continues to function easily in PHP 5, PHP 7, and PHP 8.

Example 1

Here is the basic example of the PHP String substr_count() function to count the number of times a substring comes in the given string.

<?php
   echo substr_count("helloworld","world");
?>

Output

Here is the outcome of the following code −

1

Example 2

In the below PHP code we will use the substr_count() function and count a substring in a simple string.

<?php
   // define string here
   $string = "hello world, hello universe";

   // Substring to count
   $substring = "hello";

   // Count occurrences
   $count = substr_count($string, $substring);

   // Output the count
   echo "The substring '$substring' appears $count times in the string.";
?> 

Output

This will generate the below output −

The substring 'hello' appears 2 times in the string.

Example 3

Now the below code uses the $start parameter in the substr_count() function for counting the string from a specific starting index.

<?php
   // Define string here
   $string = "banana banana banana";

   // Substring to count
   $substring = "banana";

   // Count occurrences starting from the 8th character (index 7)
   $start = 7;
   $count = substr_count($string, $substring, $start);

   // Output the count
   echo "The substring '$substring' appears $count times starting from index $start.";
?> 

Output

This will create the below output −

The substring 'banana' appears 2 times starting from index 7.

Example 4

In the following example, we are using the substr_count() function and use $start and $length to limit the search range.

<?php
   // String to search in
   $string = "abcabcabcabcabc";

   // Substring to count
   $substring = "abc";

   // Start index and length of search range
   $start = 3;  
   $length = 9; 

   // Count occurrences within the range
   $count = substr_count($string, $substring, $start, $length);

   // Output the count
   echo "The substring '$substring' appears $count times in the specified range.";
?> 

Output

Following is the output of the above code −

The substring 'abc' appears 3 times in the specified range.
php_function_reference.htm
Advertisements