CKTIS / PHP
substr_count()
Count nonoverlapping, case-sensitive occurrences within a byte window.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
substr_count(string $haystack, string $needle, int $offset = 0, ?int $length = null): intCount nonoverlapping, case-sensitive occurrences within a byte window. Restrict the window with offset and optional length; a negative length excludes bytes from its end. An empty needle raises ValueError. Overlapping occurrences are not counted twice.
Signature
substr_count(string $haystack, string $needle, int $offset = 0, ?int $length = null): int
Example output
[2,1,1]
Examples
<?php
echo json_encode([substr_count("aaaa","aa"),substr_count("ababa","aba"),substr_count("xxabABab","ab",2,-2)]);
Result
[2,1,1]