Page 1 of 1

Problem with reading txt file

Posted: Wed Jan 01, 2003 6:19 pm
by crazycheetah
Here's my PHP code to read from a file called News.txt:

Code: Select all

<?php
		$fcontents = file ('News.txt');
		while (list ($line_num, $line) = each ($fcontents)) &#123;
			$line = str_replace("/New", "<hr>", $line);
			echo $line;
			if ($line != "<hr>") &#123; echo "<br>"; &#125;
		&#125;
	?>
here's what is in the News.txt file:

Code: Select all

<b>Site Coming Soon!</b>
/New
1/1/2003 - Happy New Year!
		Happy New Years all!!!
The problem with this code is that it adds the <br> to the <hr> even with the if statements saying not to.
any help on why it does this would be greatly appreciated. thanks.

Posted: Wed Jan 01, 2003 9:30 pm
by hob_goblin
The reason it's not working right is because the line "/New" has spaces.

you would either change

Code: Select all

if ($line != "<hr>") &#123; echo "<br>"; &#125;
to

Code: Select all

if (!strpos("<hr>", $line)) &#123; echo "<br>"; &#125;
or

try this:

Code: Select all

<?

$file = file('News.txt');

	foreach($file as $key => $var)&#123;
	$var = str_replace("/New", "<hr>", $var);
	$file&#1111;"$key"] = $var;
	&#125;

	$file = implode("/n", $file);

	echo nl2br($file);

?>