In PHP, how do I extract multiple e-mail addresses from a block of text and put them into an array?

Viewed 31270

I have a block of text from which I want to extract the valid e-mail addresses and put them into an array. So far I have...

   $string = file_get_contents("example.txt"); // Load text file contents
   $matches = array(); //create array
   $pattern = '/[A-Za-z0-9_-]+@[A-Za-z0-9_-]+\.([A-Za-z0-9_-][A-Za-z0-9_]+)/'; //regex for pattern of e-mail address
   preg_match($pattern, $string, $matches); //find matching pattern

However, I am getting an array with only one address. Therefore, I am guessing I need to cycle through this process somehow. How do I do that?

7 Answers
Related