views:

55

answers:

1

I have txt file with email addresses under under the other like :

[email protected]
[email protected]

So far I managed to open it with

 $result = file_get_contents("tmp/emails.txt");
but I don't know to to get the email addresses in an array. Basically I could use explode but how do I delimit the new line ? thanks in advance for any answer !

+7  A: 

Just read the file using file() and you'll get an array containing each line of the file.

$emails = file('tmp/emails.txt');

To not append newlines to each email address, use the FILE_IGNORE_NEW_LINES flag, and to skip empty lines, use the FILE_SKIP_EMPTY_LINES flag:

$emails = file('tmp/emails.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

Doing a var_dump($emails) of the second example gives this:

array(2) {
  [0]=>
  string(13) "[email protected]"
  [1]=>
  string(14) "[email protected]"
}
BoltClock