Home >>PHP String Functions >PHP substr_count() Function
PHP substr_count() function is used to count the number of times a sub-string occurs in a given input string. It also provide an option to search for the given substring in a given range of the index. It is case-sensitive.
Syntax:
substr_count($string, $substring, $start, $length);
Parameter | Description |
---|---|
string | This is a required parameter. This parameter contains the string to check. |
substring | This is a required parameter. This parameter defines the string to search for. |
start | This is an optional parameter. This parameter defines where in string to start searching. |
length | This is an optional parameter. This parameter defines the length of the search. |
Here is an example of substr_count() function in PHP:
<html> <body> <?php echo substr_count("PHP, PHPTPOINT, php, Python, PHP Framework","PHP"); ?> </body> </html>
Here is an another example of substr_count() function in PHP:
<html> <body> <?php $str = "Hello Everyone.. I am Jerry and i love PHP.."; echo substr_count($str,"e")."<br>"; // no defined start position echo substr_count($str,"e",2)."<br>"; // start position is 2 ("l") echo substr_count($str,"e",3)."<br>"; // start position is 3 echo substr_count($str,"e",3,13)."<br>"; // start poition is 3 and length is 13 ?> </body> </html>