Open In App

PHP substr_count() Function

Improve
Improve
Like Article
Like
Save
Share
Report

The substr_count() is a built-in function in PHP and is used to count the number of times a substring occurs in a given string. The function also provides us with an option to search for the given substring in a given range of the index. It is case sensitive, i.e., “abc” substring is not present in the string “Abcab”. If the (start+length) value specified for search exceeds the size of the passed string, it returns a warning message to the user.

Syntax:

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

Parameters: This function accepts four parameters as shown in the above syntax and described below.

  1. $string – The string passed in the parameter is the one in which the substring’s occurrence is counted. This parameter is mandatory to be supplied.
  2. $substring – The substring passed in the parameter is searched is the string and the occurrence count is returned. This parameter is mandatory to be supplied.
  3. $start – This parameter is optional. If this parameter is passed, then the search is done starting from the start position instead of searching the whole string for the occurrence of a substring.
  4. $length – This parameter is optional. The parameter is dependent on start. It limits the search operation from start to start+length position. If the value of start+length increases the length of the string, then a warning message is generated


Return Value: This function can return different values as shown below in different cases.

  • The number of times the given substring appears in the string if optional parameters are not passed
  • The number of times the substring appears in the string from the start to the end position when start is passed in parameter
  • The number of times the substring appears in the string from the start to the start+length position when start and length both parameters are passed.

Examples:

Input: string= "geeks for geeks" substring="geeks" 
Output: 2
Explanation: "geeks" occurs two times in the given string 

Input: string= "geeks for geeks" substring="geeks" start=6 
Output: 1 
Explanation: "geeks" occurs one time in the given string, in 
this case search for substring starts from 6th position i.e., 
the substring is searched in "for geeks".  

Below programs illustrate the substr_count() function:

Program 1: When both optional parameters are not passed.




<?php
// PHP program to demonstrate the substr_count() function
  
$str = "geeks for geeks"
  
echo substr_count($str, "geeks");  // displays the count
  
?>


Output:

2

Program 2: When parameter $start is passed.




<?php
// PHP program to demonstrate the 
// substr_count() function
   
// $start is passed
$str = "geeks for geeks"
  
echo substr_count($str, "geeks", 6);  
  
?>


Output:

1

Program 3: When $start and $length both are passed.




<?php
// PHP program to demonstrate the 
// substr_count() function 
  
$str = "geeks for geeks"
  
echo substr_count($str, "geeks", 6, 2);  
  
?>


Output:

0

Program 4: Program to demonstrate the warning message when ($start+$length) exceeds the length of $string.




<?php
// PHP program to demonstrate the 
// substr_count() function 
  
$str = "geeks for geeks"
  
// ($start + $length ) > length of $str
echo substr_count($str, "geeks", 6, 14); 
  
?>


Output:

PHP Warning:  substr_count(): Length value 14 exceeds string length

Program 5: Program to demonstrate the substr_count() when it does not count overlapped substring.




<?php
// PHP program to demonstrate the 
// substr_count() function 
  
$str = "abcabcab"
  
echo substr_count($str, "abcab");  
  
?>


Output:

1


Last Updated : 22 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads