Search code examples
phpstrpos

Find the number of needles in a haystack


I need to find the number of times a needle appears in a haystack. Until this point I've put together the following code:

<?php

$haystack = 'http://www.google.com';
$needle = 'a';
$contents = file_get_contents($haystack);

?>

I want to find the number of times 'a' (the needle) appears in $contents (haystack - google.com's source code).

Thank you


Solution

  • substr_count is what you are looking for.