我试图做到这一点,所以每周我的php代码都会得到已经存储在一个预先生成的文本文件中的文本,每周回显一个新的行。我试过使用date()
,但结果并没有达到我的预期。
以下是代码:
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
$text = file_get_contents("lines.txt");
$text = trim($text); //This removes blank lines so that your
//explode doesn't get any empty values at the start or the end.
$array = explode(PHP_EOL, $text);
$lineNumber = count($array);
echo "<p>{$array[0]}</p>";
?>
以下是lines.txt的格式:
不停地
发布于 2016-02-15 16:51:47
如果您只需要从文本文件回显行:
$array = explode(PHP_EOL, $text);
foreach($array as $val){
echo "$val\n";
}
如果你想每周重复一条新的线路,可以在某个地方跟踪它,比如:
$counter = 0;
if(!file_exists("date.txt")){
file_put_contents("date.txt",date("d"));
}else{
$date = file_get_contents("date.txt");
$dayNow = date("d");
$counter = ($dayNow - $date)/7;
}
$text = file_get_contents("lines.txt");
$text = trim($text);
$array = explode(PHP_EOL, $text);
echo $array[$counter]."\n";
发布于 2016-02-15 16:52:23
这里有一个解决办法--如果我正确理解你的问题:
<?php
$fname = 'quoteoftheweek.txt';
if (!file_exists($fname)) {
$quote = '???'; // File does not exist
} else {
$lines = file($fname, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$nweek = (integer)date('W',time()); // Get the week number
$nlines = count($lines); // Get the number of lines
// Calculate the index as week_number modulo number_of_lines
// If number_of_lines < 1 set it to false
$index = ($nlines>0) ? ($nweek % $nlines) - 1 : false;
$quote = ($index!==false) ? $lines[$index] : '???';
}
echo '<p>Quote, week '.$nweek.' : ' . $quote . '</p>';
quoteoftheweek.txt文件的内容:
第一周报价 第二周报价 第三周报价 第四周的报价 第五周报价 第六周报价 .
结果(2016-02-15):
引用,第七周:第七周的引用
备注:
https://stackoverflow.com/questions/35414414
复制相似问题