如何将文本文件读入行数组并使用PHP显示?

时间:2022-02-21 05:29:41

I have text file that looks like this:

我有一个看起来像这样的文本文件:

1 1 1 1

1 2 3 5

4 4 5 5

I want to read this text file into array of lines and display it. Can anyone help me do this?

我想将这个文本文件读成行数组并显示它。任何人都可以帮我这样做吗?

5 个解决方案

#1


This should get you going: php function file

这应该让你去:php函数文件

#2


<?php

$cont = file_get_contents("data.txt");
$lines = explode("\n",$cont); // $lines is now an array containing each line

// do something with data here

?>

make sure you use the correct line endings however as Windows uses \r\n and UNIX uses \n.

确保使用正确的行结尾,但是Windows使用\ r \ n并且UNIX使用\ n。

#3


you can use fopen(), fgets(). see here

你可以使用fopen(),fgets()。看这里

eg

$f=fopen("file","r");
if($f){
    while (!feof($f)) {
        $line = fgets($f, 4096);
        print $line;
    }
    fclose($f);
}

#4


You'd want to do something like this:

你想做这样的事情:

<?

$filename = "somefile.txt";
$arr = file($filename);
foreach($arr as $line){
    print $line . "<br />";
}

?>

#5


This one is working for me.

这个适合我。

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

#1


This should get you going: php function file

这应该让你去:php函数文件

#2


<?php

$cont = file_get_contents("data.txt");
$lines = explode("\n",$cont); // $lines is now an array containing each line

// do something with data here

?>

make sure you use the correct line endings however as Windows uses \r\n and UNIX uses \n.

确保使用正确的行结尾,但是Windows使用\ r \ n并且UNIX使用\ n。

#3


you can use fopen(), fgets(). see here

你可以使用fopen(),fgets()。看这里

eg

$f=fopen("file","r");
if($f){
    while (!feof($f)) {
        $line = fgets($f, 4096);
        print $line;
    }
    fclose($f);
}

#4


You'd want to do something like this:

你想做这样的事情:

<?

$filename = "somefile.txt";
$arr = file($filename);
foreach($arr as $line){
    print $line . "<br />";
}

?>

#5


This one is working for me.

这个适合我。

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