Page 1 of 1
Help:read and parse an external file to generate random html
Posted: Fri Mar 07, 2008 12:02 pm
by hawkfire
I'm setting up a "Message Of The Day" using php and an external file accessible via a URL. I want to read in the file, parse it into unique topics and messages, then output an HTML box containing a random message. For example:
http://www.somewebsite.com/motd.txt contains this data:
Code: Select all
**1 This is topic 1
* This is the message for topic 1
**2 This is the second topic
* Another message
**3 Last topic
* Did you know that the message can
* be
*
* multiple lines?
The php code would:
- read in the file
- separate it into 3 rows in a 2 column array
- select one at random
- Output an html page with the topic in a <h1> tage, and the body in a suitable format.
The code would not have to update the motd.txt file.
Is this possible, and where would I begin to figure out how to do this?
Re: Help:read and parse an external file to generate random html
Posted: Fri Mar 07, 2008 12:15 pm
by Sekka
Very possible.
Firstly, the file to be read. It may be easier to do this if you place the MOTD's in an XML file? Easier to parse and seperate?
Secondly, if you go down the text file route, I would look into file_get_contents() or fopen(). Then use PHP functions to parse the document into a format you need.
Hope this gives you somewhere to start.
Re: Help:read and parse an external file to generate random html
Posted: Fri Mar 07, 2008 12:19 pm
by hawkfire
Thanks! That's an excellent starting point. Unfortunately, XML isn't an option. I'm actually using a file that is a hidden, restricted wiki article as my base so that non-web programmers can update it at their leisure (long complicated story).
Re: Help:read and parse an external file to generate random html
Posted: Fri Mar 07, 2008 12:25 pm
by Sekka
Ah ok.
Well, file_get_contents() would read the text file and return it's contents to you in a variable for use in parsing. This would be the easiest option.
As for the actual parsing, there are a number of ways you can do it. The 2 ** line, will that always be there to seperate posts, and if so, will it always be one line?
Re: Help:read and parse an external file to generate random html
Posted: Fri Mar 07, 2008 12:39 pm
by bertfour
To Start:
Code: Select all
<?
$filename = "motd.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
$quotes = explode("**", $contents);
print "<pre>";
print_r($quotes);
print "</pre>";
?>
Re: Help:read and parse an external file to generate random html
Posted: Fri Mar 07, 2008 12:44 pm
by hawkfire
Yes, so my pseudo-code would be something like:
Code: Select all
iNumberofMessages = 0
read file
while not eof {
read line
if firstTwoCharacters = "**" then
iNumberofMessages++
szMessageArray[iNumberofMessages-1,0]=line
else
szMessageArray[iNumberofMessages-1,1]+=line
endif
}
iMessageToDisplay=random(1,iNumberofMessages)
echo szPageHeader
echo "<h1>"+szMessageArray[iNumberofMessages-1,0]+"</h1>"
echo "<p>"+szMessageArray[iNumberofMessages-1,1]+"</p>"
echo szPageFooter
Re: Help:read and parse an external file to generate random html
Posted: Wed Mar 12, 2008 2:42 pm
by hawkfire
Okay, I got it to work, but with a bit of a cluge:
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>My Title</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<META content="MSHTML 6.00.2900.2963" name=GENERATOR>
</HEAD>
<BODY>
<?php
$configFile=file_get_contents('../../../wiki/data/pages/numberofpages.txt');
$somenumber=rand(1, ($configFile*1));
$filename='http://www.mydomain.com/wiki/doku.php?id=myarticle'.$somenumber.'&do=export_xhtmlbody';
echo file_get_contents($filename);
?>
<br />
</p>
</BODY></HTML>
So, instead of reading in one big file and parsing it, then outputting a random article, I did it in two steps:
1) Read in a Wiki article that only contains a number representing the number of available articles
2) Use that number as the upper boundary to get a random number, and display the corresponding article.
Probably a lot quicker than using one big file. The fopen command always gave me a blank page, even if I tried to just display the whole thing without parsing it.
Thanks everyone!