Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP file write with a new line

Tags:

php

fopen

fwrite

I'm making a chat room with a text file. Very simple, but I don't want it to put a damper on the server. Anyways. When I try to make a message, it puts the appended message on the same line like this

Alan,5/6/12,message<br>Joe,5/6/12,hello<br>

I'd like it to be

Alan,5/6/12,message
Joe,5/6/12,hello

This is what I have.

$total = 'Alan,5/6/12,hello joe<br>\r\n';
$fn = "messages.txt";
$file = fopen($fn,"a");
fwrite($fn,$total);
fclose($fn);

Thanks.

like image 469
AlanPHP Avatar asked Aug 25 '26 19:08

AlanPHP


1 Answers

$total = 'Alan,5/6/12,hello joe<br>\r\n';

would have worked if you'd used doublequotes, e.g.

$total = "Alan,5/6/12,hello joe<br>\r\n";
         ^--                           ^--

single-quoted strings do not interpret metacharacters like linebreak/newline, and all your single-quoted version does is put a literal r and n character into the output.

like image 195
Marc B Avatar answered Aug 27 '26 09:08

Marc B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!