When should one consider using preg_match_all() over preg_match() in PHP for pattern matching?

When you need to find multiple occurrences of a pattern within a string, you should consider using preg_match_all() over preg_match(). preg_match_all() will return all matches found in the string, while preg_match() will only return the first match. This is useful when you need to extract multiple pieces of information from a string based on a pattern.

$string = "Hello 123 World 456!";
$pattern = '/\d+/';

// Using preg_match_all() to find all numbers in the string
if (preg_match_all($pattern, $string, $matches)) {
    print_r($matches[0]);
}