簡體   English   中英

每天如何從文件中選擇一行?

[英]How to choose one line from a file per day?

我會解釋更多

我有文件名為date.php打來電話,文本文件word.txt 我在word.txt放了更多箴言

現在,每天我只需要從word.txt打印一個諺語,就像這樣:

  • 周六打印“一個被燒死的孩子怕火”
  • 周日打印“沒有痛苦就沒有收獲”
  • 等等,諺語每天都會改變

有人可以幫我這個想法嗎?

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

如果是一周輪換(即每個工作日一個諺語),我會這樣做:

$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];

您可以使用文件功能從文件中讀取。
假設您將新諺語放在頂部,則可以執行以下操作:

$lines = file('word.txt');
echo $lines[0]; // displays the first line
    $proverbs = file('word.txt');
    $today = (int)date('N');
    echo $proverbs[$today - 1];

只需將所有諺語放在文本文件的新行中即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM