You want to use preg_match_all() . This is how it will look in your code. The actual function returns the number of elements found, but the $matches array will contain the results:
<?php $string = "/brown fox jumped [0-9]/"; $paragraph = "The brown fox jumped 1 time over the fence. The green fox did not. Then the brown fox jumped 2 times over the fence"; if (preg_match_all($string, $paragraph, &$matches)) { echo count($matches[0]) . " matches found"; }else { echo "match NOT found"; } ?>
It will display:
Found 2 matches
Doug neiner
source share