How does the preg_match_all() function work in PHP?

The preg_match_all() function in PHP is used to perform a global regular expression match on a string and return all matches. To use this function, you need to provide a regular expression pattern, the input string to search within, and an array to store the matches found. This function is useful for extracting specific patterns or data from a string.

$string = "Hello, my name is John. I am 25 years old.";
$pattern = '/\b[a-zA-Z]+\b/';
preg_match_all($pattern, $string, $matches);

print_r($matches[0]);