如何从文件中每天选择一行?

时间:2010-07-17 13:19:35

标签: php

我会解释更多

我有一个名为date.php的文件和名为word.txt的文本文件。我在word.txt

中添加了更多箴言

现在,我每天只需要从word.txt打印一条谚语,就像这样:

  • 星期六打印“烧焦的孩子怕火”
  • 星期天打印出“没有痛苦就没有收获”
  • 等等,这句谚语每天都会改变

任何人都可以帮我解决这个问题吗?

4 个答案:

答案 0 :(得分:7)

$proverbs = file('word.txt');
echo $proverbs[(int)date('z')%count($proverbs)];

答案 1 :(得分:4)

如果它是一周rota(即每个工作日一条谚语),我会这样做:

$proverbs = array(

  # Monday 
  "Build a man a fire, and he'll be warm for a day.
   Set a man on fire, and he'll be warm for the rest of his life.
   -- Terry Pratchett", 

  # Tuesday
  "The pen is mightier than the sword if the sword is very short,
  and the pen is very sharp
  -- Terry Pratchett",

  # Wednesday
  "....",

  # Thursday
  "...."


  );

  $current_weekday = date("N"); # 1 = Monday ... 7 = Sunday

  echo $proverbs[$current_weekday];

答案 2 :(得分:0)

您可以使用file function从文件中读取 假设您将新谚语置于顶部,您可以执行以下操作:

$lines = file('word.txt');
echo $lines[0]; // displays the first line

答案 3 :(得分:0)

    $proverbs = file('word.txt');
    $today = (int)date('N');
    echo $proverbs[$today - 1];

将所有谚语放在文本文件中的新行上。