Forum Moderators: coopster

Message Too Old, No Replies

Problem checking for blankline in textfile

read textfile blank line if then else

         

Marrand

3:42 pm on Mar 9, 2005 (gmt 0)

10+ Year Member



Hi
I'm trying to read the contents of a text file and identify when there is a blank line (in order to format the content that follows).

I think my problem is the syntax, since I'm new to PHP.

To check for a blankline, I'm using the code :
<i>if ($content == "")</i>
Is that correct?

The text file is a fixture list that looks like this
---------------------------
Thursday March 17
8:15pm: RCFC v United

Sunday March 20
10am: Titans v Amigos
12pm: United v Cobras
2pm: Rcfc v Boca Jr

Thursday March 24
8:15pm: Boca Jr v Titans
etc
etc
------------------

I'm using fgets() to read line by line, and when I find a blank line, I want to make the next line bold (ie the date).

At the moment, it only makes the first date bold. I suspect that the code is not detecting any other blank lines in order to make other bolds.

The code is below.

<?php
$filename = "content/matches.txt";
$fp = fopen ($filename, "r");
$PrevLineBlank=true; // default since first line is always a date
$output = "<br/>";
while (!feof ($fp)) {
$content = fgets( $fp, filesize($filename) );
if ($PrevLineBlank==true) { // it must be the date
$output .= "<p><strong>$content</strong><br/>";
$PrevLineBlank=!$PrevLineBlank;
}
else
if ($content == "") { // must be a blank line
$output .= "</p>";
$PrevLineBlank=!$PrevLineBlank;
}
else { // must be a fixture or result
$output .= "$content<br/>";
}

}
fclose ($fp);
echo $output;
?>

[edited by: coopster at 4:37 pm (utc) on Mar. 9, 2005]
[edit reason] removed urls per TOS [webmasterworld.com] [/edit]

Marrand

7:38 pm on Mar 9, 2005 (gmt 0)

10+ Year Member



I seem to have fixed it.
Since I was using fgets() I needed to search for "\r\n" when looking for a blank line, not just "". fgets() includes the linebreak, whereas fread() doesn't.

Can anyone explain why I need to use \r\n rather than just \n, which I've used in other PHP scripts?

Warboss Alex

11:49 pm on Mar 10, 2005 (gmt 0)

10+ Year Member



\r\n is the Windows line ending; could your php site be hosted on a Windows server?

(\n is for Linux, \r for Mac)

coopster

1:38 am on Mar 11, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, Marrand, and good question!

It has come up before [webmasterworld.com], and it is no doubt one of the most difficult obstacles for any cross-platform programmer.