从文本文件PHP中读取数据

时间:2010-04-15 20:24:07

标签: php file text

我只是想知道如何在php中读取文本文件,我想让它从文本文件中显示最后200个条目(它们各自在新行中)。

John White
Jane Does
John Does
Someones Name

等等

谢谢!

5 个答案:

答案 0 :(得分:5)

使用fopenfgets,或者只使用file

答案 1 :(得分:1)

有几种方法可以从PHP中的文件中读取文本。

您可以使用fgetsfread等。将文件加载到动态数组中,然后输出该数组的最后200个元素。

答案 2 :(得分:1)

file将获取文件的内容并将其放入数组中。在那之后,就像JYelton所说,输出最后200个元素。

答案 3 :(得分:0)

输出最后200行。最后一行:

$lines = file("filename.txt");
$top200 = array_slice(array_reverse($lines),0,200);
foreach($top200 as $line)
{
    echo $line . "<br />";
}

答案 4 :(得分:0)

<?php
$myfile = fopen("file_name.txt", "r") or die("Unable to open file!");
// Output one character until end-of-file
while(!feof($myfile)) {
   echo fgetc($myfile);
}

fclose($myfile);
?>

您可以使用此