PHP substr_count() Function


The substr_count() function is used to counts the number of times a substring occurs in the given string..

Syntax
substr_count(string,substring,start,length)
Parameters
  • String - (Required) Specify the string to check.
  • substring - (Required) Specify the string to search.
  • start - (Required) Specify that start where to search,if negative start counting from the end of the string.
  • length - (Required) Specify the length of the search.
Example
<?php
    echo substr_count("Good Morning. How was a day ?,They day is Good","Good");
?>

Output

2
Example
<?php
    $a = "This day is very nice";
    echo strlen($a )."<br>";
    echo substr_count($a ,"day")."<br>"; 
    echo substr_count($a,"day",2)."<br>";
    echo substr_count($a,"is",3,3)."<br>";
?>

Output

21
1
1
0

Prev Next