What is the purpose of using preg_match_all in PHP and how does it work with arrays?

The purpose of using preg_match_all in PHP is to perform a global regular expression match on a string and return all matches. It is useful when you need to find multiple occurrences of a pattern in a string. When using preg_match_all with arrays, the matches are stored in a multidimensional array where each sub-array contains the matches for a specific capture group.

<?php
$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);
?>