What are the differences between using preg_replace and preg_match_all in PHP when dealing with regular expressions?

When dealing with regular expressions in PHP, preg_replace is used to search for a pattern and replace it with a specified string, while preg_match_all is used to search for all occurrences of a pattern within a string and store them in an array. Using preg_replace allows you to modify the original string by replacing the matched pattern, while preg_match_all simply returns an array of all occurrences without modifying the original string. Here is an example of using preg_match_all to extract all email addresses from a given string:

$string = "Contact us at email@example.com or support@example.com";
preg_match_all('/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/', $string, $matches);
print_r($matches[0]);