fork download
  1. <?php
  2. $text = '
  3. some@email.com
  4. not_email@test another@junk email@again.com
  5. @var notvar@
  6. my@fake.png
  7. some random text
  8. ';
  9.  
  10. $pattern = '~[^\s]+@[^\s]+~';
  11. $output = array();
  12.  
  13. preg_match_all($pattern, trim($text), $matches);
  14.  
  15. foreach($matches[0] as $key => $val) {
  16. $email = filter_var($val, FILTER_VALIDATE_EMAIL);
  17. if($email) {
  18. $output[] = $email;
  19. }
  20. }
  21.  
  22. print_r($output);
Success #stdin #stdout 0s 82752KB
stdin
Standard input is empty
stdout
Array
(
    [0] => some@email.com
    [1] => email@again.com
    [2] => my@fake.png
)