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.
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
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.
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.
Program 2: When parameter $start is passed.
Program 3: When $start and $length both are passed.
Program 4: Program to demonstrate the warning message when ($start+$length) exceeds the length of $string.
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.
- How to get the function name inside a function in PHP ?
- p5.js | sin() function
- p5.js | cos() function
- p5.js | log() function
- PHP | each() Function
- p5.js | arc() Function
- p5.js | tan() function
- p5.js | box() Function
- p5.js | nfs() Function
- p5.js | max() function
- PHP | Ds\Set contains() Function
- D3.js | d3.min() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.