Php – How to get the number of times a string is repeated in PHP

phpstring

In a string like "abc fox fox fox ghi xyz" how can i get the number of times 'fox' is repeated in the string?

Best Solution

$string = 'abc fox fox fox ghi xyz';

$substring = 'fox';

$substringCount = substr_count($string, $substring);

echo '"' . $substring . '" appears in "' . $string . '" ' . $substringCount . ' times';