Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line break not working when writing to text file in PHP

I have the following test script:

<?php  $myFile = "testFile.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = "Floppy Jalopy\n"; fwrite($fh, $stringData); $stringData = "Pointy Pinto\n"; fwrite($fh, $stringData); fclose($fh);  ?> 

when run however and opened usign Notepad, the data is returned in a single line without breaks as:

Floppy Jalopy(crazy box)Pointy Pinto(crazy box)

where i cant find the appropriate character for 'crazy box' but its a REALLY crazy box. WHAT GIVES!

like image 726
JM4 Avatar asked Nov 16 '10 15:11

JM4


People also ask

How do you add a new line to a text file in PHP?

Answer: Use the Newline Characters ' \n ' or ' \r\n ' You can use the PHP newline characters \n or \r\n to create a new line inside the source code. However, if you want the line breaks to be visible in the browser too, you can use the PHP nl2br() function which inserts HTML line breaks before all newlines in a string.

How do you break a line in a text file?

CR and LF are control characters or bytecode that can be used to mark a line break in a text file. CR = Carriage Return ( \r , 0x0D in hexadecimal, 13 in decimal) — moves the cursor to the beginning of the line without advancing to the next line.

How break a string from line in PHP?

The nl2br() function inserts HTML line breaks (<br> or <br />) in front of each newline (\n) in a string.

What is linefeed in PHP?

It is an in-built function of PHP, which is used to insert the HTML line breaks before all newlines in the string. Although, we can also use PHP newline character \n or \r\n inside the source code to create the newline, but these line breaks will not be visible on the browser.


1 Answers

It is best to use PHP_EOL. This is cross-platform, so it automatically chooses the correct newline character(s) for the platform PHP is currently running on.

$stringData = "Floppy Jalopy" . PHP_EOL; 

PHP Constants

like image 55
Evan Mulawski Avatar answered Sep 23 '22 02:09

Evan Mulawski