Problem with reading txt file

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
crazycheetah
Forum Newbie
Posts: 1
Joined: Wed Jan 01, 2003 6:19 pm

Problem with reading txt file

Post 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.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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);

?>
Post Reply