your question as it stands is very broad, there's a lot of question I'd ask such as is there any HTML in the quotes.txt or is it just content?
What you seem to be implying is that you have a single line from a list of lines that is displayed based on a date formula? So Quotes.txt has 52 lines of text one for each day and then a line number is picked at random?
I want to go through your question one part at a time.
$text = file("quotes.txt");
It would be better here to use file_get_contents
because that's all you need, the contents not the metadata or filedata.
$search = array ("\r\n", "\r");
$text = str_replace($search, "\n", $text);
$array = explode("\n", $text);
This part is misleading as the semi-standardisation of line endings (to \n
) is wasted in the explode
statement. I would use PHP_EOL
and ignore the string replace.
$line = date("z");
z
is the day of the year, however your question is weeks, and that is W
as the ISO-8601 week number (0 to 52) .
echo $array[$line];
This echo should be the part outputting to your browser, but this will simply output the content rather than wrap it in valid HTML, you'd have to do that your own way, basically you're just dumping data to the browser with no tags, or anything beyond the contents of that line in the array.
So a rewrite:
$text = file_get_contents("quotes.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 = date('W');
What this will output is only the data in the array line number. You can wrap it in cursory HTML as so:
print "<!DOCTYPE html>
<html lang="en">
<head>
<title>My Page!</title>
</head>
<body>
<p>";
echo $array[$lineNumber];
print "</p>
</body>
</html>";
If you have a standard HTML page already that you want to insert the data into then you can simply print
or echo
the value as you have done already and exampled above. You would possibly need to add tags such as <?php ... ?>
(to open and close the PHP section) and you would also need to save the page as .php
if it is a .html
file.