为了账号安全,请及时绑定邮箱和手机立即绑定

每天自动显示新行表单文本文件

每天自动显示新行表单文本文件

PHP
慕容708150 2021-07-10 16:01:04
我有一个文本文件,我想每天从中读取一个新行,如何每天自动更新开始和结束值?$start = 1; // array indexes are starting with zero$end = 4;   // array indexes are starting with zero$lines = file('test.txt'); // get lines of file as an arrayfor($i = $start; $i <= $end && $i < count($lines); $i++) {    echo $lines[$i];}
查看完整描述

1 回答

?
慕虎7371278

TA贡献1802条经验 获得超4个赞

由于您必须为每个访问者读取文件以显示文本的第一行,因此您可以根据需要编写一些代码来修改文件。


假设你有这个文件:


2019-05-12

This is line 1

This is line 2

This is line 3

然后为每个访问者包含这件作品:


$text = file('myfile.txt');      // read file into array

$date = $text[0];                // get the last update date

$now = date('Y-m-d',time());     // today

$show = 1;                       // 1 = first text line in array


//if we need to update

if( $date !== $now ){

     $show = 2;                  // 2 = we need a new line

     $text[0] = $now;            // set last update to today

     unset($text[1]);            // delete the old line

     // write back file without the deleted line

     file_put_contents('myfile.txt', implode("\n",$text); 

}


echo $text[$show];

没有测试代码,但你明白了


查看完整回答
反对 回复 2021-07-16
  • 1 回答
  • 0 关注
  • 161 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信