-4

I have a text file having some members information about organizational experts. Now I want to extract only email addresses from that.

Example file:

a@a.com jhgvhdhf bahau@gmail.com hdghfd  G@g.com

How could I extract all the strings ending with @hotmail.com? If the filename is foo.txt...

ohaal
  • 5,208
  • 2
  • 34
  • 53

1 Answers1

0

Load the data into a string (I'm using $str for that) and apply a email regular expression:

$pattern = '/[A-Z0-9._%+-]+@[^\.].*\.[a-z]{2,}/i';
if (preg_match($pattern, $str, $matches)){
    echo $matches[1];
}

As Guy commented below, you can load the file into a string with:

$str = file_get_contents('/path/to/file'); 

EDIT:

For hotmail, you can change the pattern to:

$pattern = '/[A-Z0-9._%+-]+@hotmail.com/i';
Expedito
  • 7,771
  • 5
  • 30
  • 43