使用php读取纯文本文件

时间:2021-07-10 21:21:39

I have a text file with this information in my server:

我的服务器上有一个包含这些信息的文本文件:

Data1
Data2
Data3
.
.
.
DataN

How do I read all the information from the text file (line by line) using PHP?

如何使用PHP逐行读取文本文件中的所有信息?

8 个解决方案

#1


64  

<?php

$fh = fopen('filename.txt','r');
while ($line = fgets($fh)) {
  // <... Do your work with the line ...>
  // echo($line);
}
fclose($fh);
?>

This will give you a line by line read.. read the notes at php.net/fgets regarding the end of line issues with Macs.

这将给你一行一行的阅读。请阅读php.net/fgets关于mac的行问题的说明。

#2


14  

http://php.net/manual/en/function.file-get-contents.php
http://php.net/manual/en/function.explode.php

http://php.net/manual/en/function.file-get-contents.php http://php.net/manual/en/function.explode.php

$array = explode("\n", file_get_contents($filename));

数组美元=爆炸(“\ n”,file_get_contents(文件名)美元);

This wont actually read it line by line, but it will get you an array which can be used line by line. There are a number of alternatives.

这实际上不会逐行读取它,但它会为您提供一个可以逐行使用的数组。有很多选择。

#3


11  

This one is working for me

这个对我有用

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

#4


7  

You can also produce array by using file:

你也可以使用文件制作阵列:

$array = file('/path/to/text.txt');

#5


6  

$filename = "fille.txt";
$fp = fopen($filename, "r");

$content = fread($fp, filesize($filename));
$lines = explode("\n", $content);
fclose($fp);
print_r($lines);

In this code full content of the file is copied to the variable $content and then split it into an array with each newline character in the file.

在这段代码中,文件的完整内容被复制到变量$content中,然后将其分割为一个数组,其中包含文件中的每个换行字符。

#6


4  

W3Schools is your friend: http://www.w3schools.com/php/func_filesystem_fgets.asp

W3Schools是您的朋友:http://www.w3schools.com/php/func_filesystem_fgets.asp

And here: http://php.net/manual/en/function.fopen.php has more info on fopen including what the modes are.

这里:http://php.net/manual/en/function.fopen.php有更多关于fopen的信息,包括模式是什么。

What W3Schools says:

什么W3Schools说:

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

while(! feof($file))
  {
  echo fgets($file). "<br />";
  }

fclose($file);
?> 

fopen opens the file (in this case test.txt with mode 'r' which means read-only and places the pointer at the beginning of the file)

fopen打开文件(在本例中为测试)。txt模式为'r',表示只读,将指针放在文件的开头)

The while loop tests to check if it's at the end of file (feof) and while it isn't it calls fgets which gets the current line where the pointer is.

while循环测试它是否在file (feof)的末尾,虽然不是,但它调用fgets,获得指针所在的当前行。

Continues doing this until it is the end of file, and then closes the file.

继续这样做,直到文件结束,然后关闭文件。

#7


2  

Try something like this:

试试这样:

$filename = 'file.txt';

$data = file($filename);
foreach ($data as $line_num=>$line)
{
    echo 'Line # <b>'.$line_num.'</b>:'.$line.'<br/>';
}

#8


1  

$file="./doc.txt";
$doc=file_get_contents($file);

$line=explode("\n",$doc);
foreach($line as $newline){
    echo '<h3 style="color:#453288">'.$newline.'</h3><br>';

}

#1


64  

<?php

$fh = fopen('filename.txt','r');
while ($line = fgets($fh)) {
  // <... Do your work with the line ...>
  // echo($line);
}
fclose($fh);
?>

This will give you a line by line read.. read the notes at php.net/fgets regarding the end of line issues with Macs.

这将给你一行一行的阅读。请阅读php.net/fgets关于mac的行问题的说明。

#2


14  

http://php.net/manual/en/function.file-get-contents.php
http://php.net/manual/en/function.explode.php

http://php.net/manual/en/function.file-get-contents.php http://php.net/manual/en/function.explode.php

$array = explode("\n", file_get_contents($filename));

数组美元=爆炸(“\ n”,file_get_contents(文件名)美元);

This wont actually read it line by line, but it will get you an array which can be used line by line. There are a number of alternatives.

这实际上不会逐行读取它,但它会为您提供一个可以逐行使用的数组。有很多选择。

#3


11  

This one is working for me

这个对我有用

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

#4


7  

You can also produce array by using file:

你也可以使用文件制作阵列:

$array = file('/path/to/text.txt');

#5


6  

$filename = "fille.txt";
$fp = fopen($filename, "r");

$content = fread($fp, filesize($filename));
$lines = explode("\n", $content);
fclose($fp);
print_r($lines);

In this code full content of the file is copied to the variable $content and then split it into an array with each newline character in the file.

在这段代码中,文件的完整内容被复制到变量$content中,然后将其分割为一个数组,其中包含文件中的每个换行字符。

#6


4  

W3Schools is your friend: http://www.w3schools.com/php/func_filesystem_fgets.asp

W3Schools是您的朋友:http://www.w3schools.com/php/func_filesystem_fgets.asp

And here: http://php.net/manual/en/function.fopen.php has more info on fopen including what the modes are.

这里:http://php.net/manual/en/function.fopen.php有更多关于fopen的信息,包括模式是什么。

What W3Schools says:

什么W3Schools说:

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

while(! feof($file))
  {
  echo fgets($file). "<br />";
  }

fclose($file);
?> 

fopen opens the file (in this case test.txt with mode 'r' which means read-only and places the pointer at the beginning of the file)

fopen打开文件(在本例中为测试)。txt模式为'r',表示只读,将指针放在文件的开头)

The while loop tests to check if it's at the end of file (feof) and while it isn't it calls fgets which gets the current line where the pointer is.

while循环测试它是否在file (feof)的末尾,虽然不是,但它调用fgets,获得指针所在的当前行。

Continues doing this until it is the end of file, and then closes the file.

继续这样做,直到文件结束,然后关闭文件。

#7


2  

Try something like this:

试试这样:

$filename = 'file.txt';

$data = file($filename);
foreach ($data as $line_num=>$line)
{
    echo 'Line # <b>'.$line_num.'</b>:'.$line.'<br/>';
}

#8


1  

$file="./doc.txt";
$doc=file_get_contents($file);

$line=explode("\n",$doc);
foreach($line as $newline){
    echo '<h3 style="color:#453288">'.$newline.'</h3><br>';

}