PHP substr_count Function | Count of Occurrences of Substring

PHP substr_count is an inbuilt String Function in PHP. It calculates the number of occurrences of a substring in another string. Also, the function provides an option to search the substring within a range in the string. In this article, we will discuss the PHP substr_count Function. Also, we will discuss a few examples of using it.

Note: The function is case-sensitive. Therefore, it treats uppercase and lowercase characters differently.

Syntax

substr_count ($string, $subString, $start, $length)

Parameters

The substr_count Function in PHP expects four parameters. Two of them are mandatory and the other two are optional.

  • $string: The string in which you want to search for the substring. Also, it is a mandatory parameter.
  • $subString: The substring which you want to search in the string. It is also a mandatory parameter.
  • $start: The third parameter refers to the starting index of search. However, it is an optional parameter with default value as 0.
  • $length: This parameter is optional and dependant on $start. It refers to the maximum length upto which the function searches. However, if ($start + $length) is greater than the string length, the function issues a warning.

Return Value

The PHP substr_count Function returns the integer count of number of occurrences of the substring in the string.

Examples

For instance, consider the following examples of using the PHP substr_count function.

Example 1: When Start and Length are not Provided

For example, if we don’t provide the start and length parameters, the function searches the whole string for the substring.

<?php
$string = "Hey.! Let's check what substr_count function does, it is a string function. substr is also another string function of PHP";

$countChars = substr_count($string, 'function');
print_r($countChars);

?>

In the above example, the substring ‘function’ appears 3 times in the main string.

Output:
3

Example 2: Providing start and Length

Also, you can provide the start and length parameters to the function

<?php
$string = "Hey.! Let's check what substr_count function does, it is a string function. substr is also another string function of PHP";

$countChars = substr_count($string, 'function' ,100, 20);
print_r($countChars);

?>

In the above example, the function searches the string starting from 100th character upto the next 20. The substring appears only once in that range.

Output:
1

Example 3: Searching From Back

Similarly, we can also search starting from the back by providing a negative start value.

<?php

$string = "Hey.! Let's check what substr_count function does, it is a string function. substr is also another string function of PHP";

$countChars = substr_count($string, 'function' ,-70);
print_r($countChars);

?>

Starting from the 70th character from the back, the substring appears twice in the string.

Output:
2

Example 4: Starting From Back With Length

Also, you can provide the length parameter when searching from the back.

<?php

$string = "Hey.! Let's check what substr_count function does, it is a string function. substr is also another string function of PHP";

$countChars = substr_count($string, 'function' ,-70, -10);
print_r($countChars);

?>
Output:
1

Conclusion

In conclusion, we discussed the PHP substr_count Function. Also, you can read more about it on the Official Documentation of PHP. Alternatively, you can also read more about PHP String Functions on Concatly.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *