Help:read and parse an external file to generate random html

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hawkfire
Forum Newbie
Posts: 14
Joined: Sun Oct 30, 2005 6:20 am

Help:read and parse an external file to generate random html

Post 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:
  1. read in the file
  2. separate it into 3 rows in a 2 column array
  3. select one at random
  4. 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?
User avatar
Sekka
Forum Commoner
Posts: 91
Joined: Mon Feb 18, 2008 10:25 am
Location: Huddersfield, West Yorkshire, UK

Re: Help:read and parse an external file to generate random html

Post 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.
hawkfire
Forum Newbie
Posts: 14
Joined: Sun Oct 30, 2005 6:20 am

Re: Help:read and parse an external file to generate random html

Post 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).
User avatar
Sekka
Forum Commoner
Posts: 91
Joined: Mon Feb 18, 2008 10:25 am
Location: Huddersfield, West Yorkshire, UK

Re: Help:read and parse an external file to generate random html

Post 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?
bertfour
Forum Commoner
Posts: 45
Joined: Fri Mar 07, 2008 7:33 am

Re: Help:read and parse an external file to generate random html

Post 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>";
?>
hawkfire
Forum Newbie
Posts: 14
Joined: Sun Oct 30, 2005 6:20 am

Re: Help:read and parse an external file to generate random html

Post 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
 
 
hawkfire
Forum Newbie
Posts: 14
Joined: Sun Oct 30, 2005 6:20 am

Re: Help:read and parse an external file to generate random html

Post 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!
Post Reply