What are the advantages of using preg_match_all over preg_match in PHP, especially when dealing with multiple occurrences of a pattern within a string?

When dealing with multiple occurrences of a pattern within a string in PHP, using preg_match_all is advantageous over preg_match because preg_match_all will return all matches found in the string, rather than just the first match. This is useful when you need to extract all occurrences of a pattern from a string for further processing or analysis.

// Example code snippet using preg_match_all to extract all occurrences of a pattern from a string
$string = "The quick brown fox jumps over the lazy dog";
$pattern = "/\b\w{4}\b/"; // Match 4-letter words

if (preg_match_all($pattern, $string, $matches)) {
    foreach ($matches[0] as $match) {
        echo $match . " ";
    }
}