Newline in a .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
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Newline in a .txt file

Post by oscardog »

Well i've coded it all, and it all works perfectly(almost) they basically have a .txt file and they can add notes to that file. Im using a while loop to output the contents of the .txt file, but its all being classed as one line.

So when i put the note into the .txt file it looks like this:

Note1Note2Note3Note4

BUT when they put a new note in i want it to do...

Note1
Note2
Note3
Note4

I was wondering if you use "/n" but thats just a newline in the HTML code i thought so yeh any help? :)
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Newline in a .txt file

Post by Eran »

Actually <br /> is a line break in HTML. \n is a line-break in plain text, so you should put it where you want lines to break.
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Newline in a .txt file

Post by oscardog »

So my code should look like this:

(Add_note.php):

Code: Select all

$note = $_POST['note'];
$note = $note . "/n";
$handle = fopen($filename, 'a'); 
fwrite($handle, $note);
fclose($handle);
Then to display..:

Code: Select all

while(!feof($file))
  {
  echo "<tr>";
  echo "<td width=\"30\">$notecounter .</td>";
  echo "<td><div align=\"left\">" . fgets($file). "</div></td>";
  echo "</tr>";
  $notecounter++;
  }
fclose($file);
And then that will display one 'note' at a time?

EDIT: That didnt work, it just outputted it all as one line again...
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Newline in a .txt file

Post by Eran »

It's "\n" not "/n"
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Newline in a .txt file

Post by oscardog »

I should read more carefully, thanks it works now :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Newline in a .txt file

Post by John Cartwright »

There is always the cross-platform PHP_EOL constant.
Post Reply