开发者

Read Data From Text File PHP

开发者 https://www.devze.com 2022-12-27 14:58 出处:网络
I\'m just wondering how I 开发者_开发百科can read a text file in php, I\'d like to have it display the last 200 entries (their each on a new line) from the text file.

I'm just wondering how I 开发者_开发百科can read a text file in php, I'd like to have it display the last 200 entries (their each on a new line) from the text file.

Like

John White
Jane Does
John Does
Someones Name

and so on

Thanks!


Use fopen and fgets, or possibly just file.


There are several methods for reading text from files in PHP.

You could use fgets, fread, etc. Load the file into a dynamic array, then just output the last 200 elements of that array.


file will get the contents of a file and put it into an array. After that, it's like JYelton said, output the last 200 elements.


This outputs last 200 rows. Last row first:

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


<?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);
?>

You may use this

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号