php 读取txt文件的每一行到新的数组元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6159683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:31:36  来源:igfitidea点击:

Read each line of txt file to new array element

phparraystext-filesfgets

提问by Dan

I am trying to read every line of a text file into an array and have each line in a new element.
My code so far.

我试图将文本文件的每一行读入一个数组,并将每一行放在一个新元素中。
我的代码到目前为止。

<?php
$file = fopen("members.txt", "r");
$i = 0;
while (!feof($file)) {

$line_of_text = fgets($file);
$members = explode('\n', $line_of_text);
fclose($file);

?>

回答by Yanick Rochon

If you don't need any special processing, thisshould do what you're looking for

如果您不需要任何特殊处理,应该可以满足您的要求

$lines = file($filename, FILE_IGNORE_NEW_LINES);

回答by Drew Dello Stritto

The fastest way that I've found is:

我找到的最快的方法是:

// Open the file
$fp = @fopen($filename, 'r'); 

// Add each line to an array
if ($fp) {
   $array = explode("\n", fread($fp, filesize($filename)));
}

where $filename is going to be the path & name of your file, eg. ../filename.txt.

其中 $filename 将是您的文件的路径和名称,例如。../文件名.txt。

Depending how you've set up your text file, you'll have might have to play around with the \n bit.

根据您设置文本文件的方式,您可能需要使用 \n 位。

回答by RoLife

Just use this:

只需使用这个:

$array = explode("\n", file_get_contents('file.txt'));

回答by iianfumenchu

$yourArray = file("pathToFile.txt", FILE_IGNORE_NEW_LINES);

FILE_IGNORE_NEW_LINESavoid to add newline at the end of each array element
You can also use FILE_SKIP_EMPTY_LINESto Skip empty lines

FILE_IGNORE_NEW_LINES避免在每个数组元素的末尾添加换行符
您也可以使用FILE_SKIP_EMPTY_LINES跳过空行

reference here

参考这里

回答by Gaurav

<?php
$file = fopen("members.txt", "r");
$members = array();

while (!feof($file)) {
   $members[] = fgets($file);
}

fclose($file);

var_dump($members);
?>

回答by Rafe Kettler

$lines = array();
while (($line = fgets($file)) !== false)
    array_push($lines, $line);

Obviously, you'll need to create a file handle first and store it in $file.

显然,您需要先创建一个文件句柄并将其存储在$file.

回答by CodeBrauer

It's just easy as that:

就这么简单:

$lines = explode("\n", file_get_contents('foo.txt'));

file_get_contents()- gets the whole file as string.

file_get_contents()- 获取整个文件作为字符串。

explode("\n")- will split the string with the delimiter "\n"- what is ASCII-LF escape for a newline.

explode("\n")- 将用分隔符分割字符串"\n"- 换行符的 ASCII-LF 转义是什么。

But pay attention - check that the file has UNIX-Line endings.

但请注意 - 检查文件是否具有UNIX-Line 结尾。

if "\n"will not work properly you have a other coding of2 newline and you can try "\r\n", "\r"or "\025"

如果"\n"不能正常工作,你有一个 2 换行符的其他编码,你可以尝试"\r\n""\r"或者"\025"

回答by Attila Nagy

$file = __DIR__."/file1.txt";
$f = fopen($file, "r");
$array1 = array();

while ( $line = fgets($f, 1000) )
{
    $nl = mb_strtolower($line,'UTF-8');
    $array1[] = $nl;
}

print_r($array);

回答by altayevrim

    $file = file("links.txt");
print_r($file);

This will be accept the txt file as array. So write anything to the links.txt file (use one line for one element) after, run this page :) your array will be $file

这将接受 txt 文件作为数组。因此,在运行此页面之后,将任何内容写入 links.txt 文件(一个元素使用一行):) 您的数组将是 $file

回答by Nick Andren

You were on the right track, but there were some problems with the code you posted. First of all, there was no closing bracket for the while loop. Secondly, $line_of_text would be overwritten with every loop iteration, which is fixed by changing the = to a .= in the loop. Third, you're exploding the literal characters '\n' and not an actual newline; in PHP, single quotes will denote literal characters, but double quotes will actually interpret escaped characters and variables.

您走在正确的轨道上,但是您发布的代码存在一些问题。首先,while 循环没有右括号。其次,每次循环迭代都会覆盖 $line_of_text,这是通过在循环中将 = 更改为 .= 来修复的。第三,您正在分解文字字符 '\n' 而不是实际的换行符;在 PHP 中,单引号将表示文字字符,但双引号实际上会解释转义字符和变量。

    <?php
        $file = fopen("members.txt", "r");
        $i = 0;
        while (!feof($file)) {
            $line_of_text .= fgets($file);
        }
        $members = explode("\n", $line_of_text);
        fclose($file);
        print_r($members);
    ?>