PHP | substr_count() Function

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

2

Program 2: When parameter $start is passed.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

1

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

0

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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); 
  
?>

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

1


My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.