PHP | substr_count() Function - GeeksforGeeks

23 Mar, 2018
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.
$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.$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.$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.$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
$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.
$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.
$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.
$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 $strecho 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
PHP-string
PHP
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to fetch data from localserver database and display on HTML table using PHP ?
PHP str_replace() Function
How to create admin login page using PHP?
Different ways for passing data to view in Laravel
Create a drop-down list that options fetched from a MySQL database in PHP
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?