猿问

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

我有一个文本文件,我想每天从中读取一个新行,如何每天自动更新开始和结束值?


$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 array

for($i = $start; $i <= $end && $i < count($lines); $i++) {

    echo $lines[$i];

}


慕容708150
浏览 162回答 1
1回答

慕虎7371278

由于您必须为每个访问者读取文件以显示文本的第一行,因此您可以根据需要编写一些代码来修改文件。假设你有这个文件:2019-05-12This is line 1This is line 2This is line 3然后为每个访问者包含这件作品:$text = file('myfile.txt');&nbsp; &nbsp; &nbsp; // read file into array$date = $text[0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // get the last update date$now = date('Y-m-d',time());&nbsp; &nbsp; &nbsp;// today$show = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 1 = first text line in array//if we need to updateif( $date !== $now ){&nbsp; &nbsp; &nbsp;$show = 2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 2 = we need a new line&nbsp; &nbsp; &nbsp;$text[0] = $now;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // set last update to today&nbsp; &nbsp; &nbsp;unset($text[1]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // delete the old line&nbsp; &nbsp; &nbsp;// write back file without the deleted line&nbsp; &nbsp; &nbsp;file_put_contents('myfile.txt', implode("\n",$text);&nbsp;}echo $text[$show];没有测试代码,但你明白了
随时随地看视频慕课网APP
我要回答