Php explode new line contents from txt file

I have a txt file with email addresses under another, for example:

test@test.com 
test2@test.com

So far I have managed to open it with

 $ result = file_get_contents ("tmp / emails.txt");
but I don’t know to get the email addresses in the array. Basically, I could use an explosion, but how can I distinguish a new line? in advance for any response!
+5
source share
3 answers

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

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

, FILE_IGNORE_NEW_LINES, FILE_SKIP_EMPTY_LINES:

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

var_dump($emails) :

array(2) {
  [0]=>
  string(13) "test@test.com"
  [1]=>
  string(14) "test2@test.com"
}
+25
$lines = preg_split('/\r\n|\n|\r/', trim(file_get_contents('file.txt')));
+4

As it seems crazy, executing returneither enterinside the double quote ( "") delimits a new line. To make this clear, enter:

explode("", "Stuff to delimit");

and just hit return in the middle ""so you get:

explode("

", "stuff to delimit");

and it works. Perhaps unconventional and can only work on Linux. But it works.

+2
source

All Articles