php explode new lines content from a txt file
I have txt file with email addresses under under the other like :
test@test.com test2@test.com
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 !
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) "test@test.com"
[1]=>
string(14) "test2@test.com"
}
$lines = preg_split('/\r\n|\n|\r/', trim(file_get_contents('file.txt')));
As crazy as this seems, doing a return
or enter
inside a double-quote (""
) delimits a newline. To make it clear, type in:
explode("", "Stuff to delimit");
and simply hit return at the middle of ""
, so you get:
explode("
", "stuff to delimit");
and it works. Probably unconventional, and might only work on Linux. But it works.
精彩评论