Hi y'all

I'm working on the read function for my guestbook, but I'm getting a false
positive on the check for the date string. It reads a text file, checks
for line headers, and then writes the HTML according to what it finds ...
really simple, and works beautifully, except that I get an extra date at
the end, which I can't figure out where comes from.

The text file format looks like this:

** guestbook.txt sample **
#date: 2001-10-13 20:23:34 +0200
#email: [EMAIL PROTECTED]
#name: Birger Stenstrup
#from: city, country
#message: FLudder pludder sludder mudder. FLudder pludder sludder
mudder.FLudder pludder 
#link: www.somelink.ext
#comment: an response from webmaster.
#--
#date: 2003-01-05 21:17:18 +0100
#name: Knap Knapstrup
#from: city, country
#message: Adder badder, pladder madder. Adder badder, pladder madder.
Adder badder, pladder 
#link: www.anotherlink.ext
#--
** end guestbook.txt **

The #email, #from, and #link lines are optionals by the user. #comment is
an option so that I can add comments or answers to entries.

The PHP part is as below (mail width causes some excessive wrapping,
please try and have your eyes compensate for that):

<?php
  // read the guestbook source file
  // version 1
  $fp = fopen("guestbook.txt","r");
        
  while (! feof($fp)) {
    $line = fgets($fp, 4096);
    switch ($line) {
      case strpos($line,"#date:") == 0 && strpos($line,"#date:") !==
false:
        $print = strtr(substr($line, 7),"\r\n","  ");
        $print = date("j F Y H.i.s O",strtotime(trim($print)));
        echo("<span class=\"ylwbld\">On $print, ");
        break;
      case strpos($line,"#email:") == 0 && strpos($line,"#email:") !==
false:
        $emailfound = true;
        $print = strtr(substr($line, 8),"\r\n","  ");
        echo("<a href=\"mailto:".trim($print)."\" class=\"ylwlink\">");
        break;
      case strpos($line,"#name:") == 0 && strpos($line,"#name:") !==
false:
        $print = strtr(substr($line, 7),"\r\n","  ");
        echo(trim($print));
        if ($emailfound) {
          echo("</a>");
        }
        break;
      case strpos($line,"#from:") == 0 && strpos($line,"#from:") !==
false:
        $print = strtr(substr($line, 6),"\r\n","  ");
        echo(" of ".trim($print));
        break;
      case strpos($line,"#message:") == 0 && strpos($line,"#message:") !==
false:
        $print = strtr(substr($line, 9),"\r\n","  ");
        echo(" wrote:</span><br>\n");
        echo("<span class=\"whttxt\">".trim($print)."</span><br>\n");
        break;
      case strpos($line,"#link:") == 0 && strpos($line,"#link:") !==
false:
        $print = strtr(substr($line, 6),"\r\n","  ");
        echo("<span class=\"ylwbld\">Link:</span> ");
        echo("<a href=\"http://".trim($print)."\" target=\"_new\"
class=\"ylwlink\">".trim($print)."</a><br>\n");
        break;
      case strpos($line,"#comment:") == 0 && strpos($line,"#comment:") !==
false:
        $print = strtr(substr($line, 9),"\r\n","  ");
        echo("<span class=\"redbld\">And the webmaster responded:
".trim($print)."</span><br>\n");
        break;
      case strpos($line,"#--") == 0 && strpos($line,"#--") !== false:
        $print = strtr(substr($line, 3),"\r\n","  ");
        echo("<hr width=\"85%\" color=\"#ffffff\" noshade
align=\"center\">\n");
        break;
    }
  }
        
  fclose($fp);
?>

It interprets the text correctly 100%, BUT I get a false positive date
check at the end of the file, which results in an extra (today's) date.
Extract of resulting HTML (last part):

<span class="ylwbld">On 5 January 2003 21.17.18 +0100, Knap Knapstrup</a>
of city, country wrote:</span><br>
<span class="whttxt">Adder badder, pladder madder. Adder badder, pladder
madder. Adder badder, pladder madder. Adder badder, pladder madder. Adder
badder, pladder madder. Adder badder, pladder madder. Adder badder,
pladder madder. Adder badder, pladder madder. Adder badder, pladder
madder. Adder badder, pladder madder.</span><br>
<span class="ylwbld">Link:</span> <a href="http://www.anotherlink.ext";
target="_new" class="ylwlink">www.anotherlink.ext</a><br>
<hr width="85%" color="#ffffff" noshade align="center">
<span class="ylwbld">On 17 March 2003 00.00.00 +0100,     

^^^NOTE THE LAST LINE!!!
It's not supposed to be there ... Can anyone tell me how to avoid it, and
possibly what causes it???

Side-remark: The text-file is to be written from entries in a form I
haven't coded yet. Like I said in the top, some of the fields are
optional, and the read script is made to compensate for this.

TIA

Rene
-- 
Rene Brehmer

This message was written on 100% recycled spam.

Come see! My brand new site is now online!
http://www.metalbunny.net

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to