substr_count()
Find how many times one string occurs in another. Use the third parameter to set the character position to start searching. With the fourth parameter you can limit how many characters to search if you don’t want it to go to the end of the string.
$str = "But if I don't leave now I'll never leave."; $c = substr_count($str, 'leave'); // 2 $c = substr_count($str, 've'); // 3 $c = substr_count($str, 'i'); // 1 $c = substr_count($str, 'I'); // 2 // only searches "don't leave now I'll never leave." $c = substr_count($str, 'I', 9); // 1 // only searches "don't" $c = substr_count($str, 'I', 9, 5); // 0
This function will not find “overlapping” strings. If if finds a match it continues looking from the end of that matching substring. In the following example, it finds a match starting at position 0 and ending at position 3. This leaves “bcA” left to search and no match is found there.
$c = substr_count('AbcAbcA', 'AbcA'); // 1
See the manual entry for substr_count()
