PHP substr_count() Function

PHP substr_compare() Function
PHP substr_replace() Function

what is the syntax of the SUBSTR_COUNT() function in php?

substr_count(string,substring,start,length)
ParameterDescription
stringRequired. Specifies the string to check
substringRequired. Specifies the string to search for
startOptional. Specifies where in string to start searching. If negative, it starts counting from the end of the string
lengthOptional. Specifies the length of the search
PHP SUBSTR_COUNT() method

examples of the SUBSTR_COUNT() function

Example 1. In this example, we count the number of times “world” occurs in the string.

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

Example 2. In this example, we use all parameters.

<?php
$str = "This is nice";
echo strlen($str)."<br>"; // Using strlen() to return the string length
echo substr_count($str,"is")."<br>"; // The number of times "is" occurs in the string
echo substr_count($str,"is",2)."<br>"; // The string is now reduced to "is is nice"
echo substr_count($str,"is",3)."<br>"; // The string is now reduced to "s is nice"
echo substr_count($str,"is",3,3)."<br>"; // The string is now reduced to "s i"
?>

Example 3. In this example, we overlapped substrings.

<?php
$str = "abcabcab";
echo substr_count($str,"abcab"); // This function does not count overlapped substrings
?>

Example 3. In this example, if we the start and length parameters exceeds the string length, this function will output a warning.

<?php
echo $str = "This is nice";
substr_count($str,"is",3,9);
?>
PHP substr_compare() Function
PHP substr_replace() Function
en English
X
Scroll to Top