首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >抢短信和Echo?

抢短信和Echo?
EN

Stack Overflow用户
提问于 2016-02-15 16:33:30
回答 2查看 44关注 0票数 0

我试图做到这一点,所以每周我的php代码都会得到已经存储在一个预先生成的文本文件中的文本,每周回显一个新的行。我试过使用date(),但结果并没有达到我的预期。

以下是代码:

代码语言:javascript
运行
复制
<?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的格式:

  1. Hello1
  2. Hello2
  3. Hello3

不停地

EN

回答 2

Stack Overflow用户

发布于 2016-02-15 16:51:47

如果您只需要从文本文件回显行:

代码语言:javascript
运行
复制
$array = explode(PHP_EOL, $text);
foreach($array as $val){
    echo "$val\n";
}

如果你想每周重复一条新的线路,可以在某个地方跟踪它,比如:

代码语言:javascript
运行
复制
$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";
票数 0
EN

Stack Overflow用户

发布于 2016-02-15 16:52:23

这里有一个解决办法--如果我正确理解你的问题:

代码语言:javascript
运行
复制
<?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):

引用,第七周:第七周的引用

备注:

  • 解决方案将文本文件直接读入数组中。 在一个步骤中删除行提要,跳过空行。
  • 它将索引作为周数模数计算到这个数组中。因此,如果行数少于数周,则重新使用行。
  • 如果文本文件应该是空的,或者不存在,它会显示‘??’。而不是引用一句。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35414414

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档