What is the purpose of using preg_match_all in PHP?

When working with PHP, sometimes we need to extract multiple occurrences of a specific pattern from a string. This is where the preg_match_all function comes in handy. It allows us to search a string for all matches of a regular expression pattern and store them in an array for further processing.

$string = "The quick brown fox jumps over the lazy dog";
$pattern = "/\b\w{4}\b/"; // Match 4-letter words

preg_match_all($pattern, $string, $matches);

print_r($matches[0]);