我会解释更多
我有一个名为date.php
的文件和名为word.txt
的文本文件。我在word.txt
现在,我每天只需要从word.txt
打印一条谚语,就像这样:
任何人都可以帮我解决这个问题吗?
答案 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];
将所有谚语放在文本文件中的新行上。