PHP substr_count() Function

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

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.

This function provides the facility to calculate that how many times a substring has occurred in the main string? It also provides an option to search a substring in a given range of the index. If the start and length values, which are specified for search are greater than the passed string, then it returns a warning to the user. PHP 4 and above versions support this function.

<?php
echo substr_count("Hello world. The world is nice","world");
?>
Output
2

Recommended Posts For You

Spread the love