php从txt文件中爆炸新行内容

时间:2022-09-25 16:52:02

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

我有txt文件,电子邮件地址在另一个下面,如:

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 !

3 个解决方案

#1


26  

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

只需使用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:

要不向每个电子邮件地址附加换行符,请使用FILE_IGNORE_NEW_LINES标志,要跳过空行,请使用FILE_SKIP_EMPTY_LINES标志:

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

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

执行第二个示例的var_dump($ emailils)可以得到:

array(2) {
  [0]=>
  string(13) "test@test.com"
  [1]=>
  string(14) "test2@test.com"
}

#2


5  

$lines = preg_split('/\r\n|\n|\r/', trim(file_get_contents('file.txt')));

#3


2  

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.

它的工作原理。可能是非传统的,可能只适用于Linux。但它的确有效。

#1


26  

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

只需使用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:

要不向每个电子邮件地址附加换行符,请使用FILE_IGNORE_NEW_LINES标志,要跳过空行,请使用FILE_SKIP_EMPTY_LINES标志:

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

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

执行第二个示例的var_dump($ emailils)可以得到:

array(2) {
  [0]=>
  string(13) "test@test.com"
  [1]=>
  string(14) "test2@test.com"
}

#2


5  

$lines = preg_split('/\r\n|\n|\r/', trim(file_get_contents('file.txt')));

#3


2  

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.

它的工作原理。可能是非传统的,可能只适用于Linux。但它的确有效。