substr_count() behaves identically in PHP 8.4 and PHP 8.5. The official PHP documentation does not list any changes for this function in either version. Its most recent significant change was in PHP 8.0.0.
Function
substr_count() counts the number of times a substring (needle) occurs within a string (haystack). The search is case sensitive.
Signature:
substr_count(
string $haystack,
string $needle,
int $offset = 0,
?int $length = null
): int
Key Parameters:
→$offset: The position to start counting from. A negative value starts counting from the end of the string.
→$length: The maximum length after the offset to search. A negative value counts from the end of the haystack.
→Return Value: Returns an integer representing the number of occurrences found.
Example
A straightforward example is counting how many times a word appears in a sentence:
<?php
$text = This is a test ;// Counts the case-sensitive occurrence of "is"
echo substr_count($text, is );// Outputs: 2
?>
The function finds "is" in "This" and "is", returning 2.