Php 如何每天从文件中选择一行?
我会详细解释 我有一个名为Php 如何每天从文件中选择一行?,php,Php,我会详细解释 我有一个名为date.php的文件和一个名为word.txt的文本文件。我在word.txt 现在,我每天只需要从word.txt打印一条谚语,如下所示: $lines = file('word.txt'); echo $lines[0]; // displays the first line $proverbs = array( # Monday "Build a man a fire, and he'll be warm for a day. Set a ma
date.php
的文件和一个名为word.txt
的文本文件。我在word.txt
现在,我每天只需要从word.txt
打印一条谚语,如下所示:
$lines = file('word.txt');
echo $lines[0]; // displays the first line
$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 = 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 = 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];
只需将所有谚语放在文本文件的新行中。是每个日历日还是每个工作日一个谚语?但我需要文本而不是数组中的谚语然后使用php的文件函数将其加载到数组中。