The substr_count() function is a built-in function in PHP which is used counts the number of times a substring occurs in a string.

Note:

This function does not count overlapped substrings. The substring is case-sensitive.

Syntax:

substr_count(string,substring,start,length)

string – This parameter is required. It specifies the string to check.

substring – This parameter is required. The substring passed in the parameter is searched is the string and the occurrence count is returned.

start – This parameter is optional. It Specifies where to begin searching in the string.

length – This parameter is optional. It specifies the length of the search.

Example:

<?php
$str = "Welcome to Tutorials!";
$str1="abcabcabc";
echo substr_count($str,'o'); //Output:3
echo "<br>";
echo substr_count($str,'co',3,3); //Output:1
echo "<br>";
echo substr_count($str1,"abcab"); //Output:1
?>

 

 

Categorized in: