Writing a New Line to File in PHP (Line Feed)

Writing a new line to file in PHP (line feed)

Replace '\n' with "\n". The escape sequence is not recognized when you use '.

See the manual.

For the question of how to write line endings, see the note here. Basically, different operating systems have different conventions for line endings. Windows uses "\r\n", unix based operating systems use "\n". You should stick to one convention (I'd chose "\n") and open your file in binary mode (fopen should get "wb", not "w").

Line break not working when writing to text file in PHP

If you want to open the file in Windows notepad, you must use Windows line breaks: \r\n

Making new line in php write to file

It is the best to use PHP_EOL. It's cross-platform and will automatically choose the correct newline character(s) for the platform PHP is running on.

$txt = "Goofy".PHP_EOL;

New line in PHP output in a text file

As other answers state, you need to add in an end of line character after each field.

Different OS's use different line endings, though, and so a "\n" may not display as a new line on Windows, for example. As Mahdi said, you can use Windows style "\r\n" line endings, or you can use the PHP_EOL constant so that line endings appropriate to the server will be output, in which case your code would look like

$data = $_POST['name'] . PHP_EOL;
$data .= $_POST['email'] . PHP_EOL;
$data .= $_POST['message'] . PHP_EOL;

Append to next line in text file using php

Try

$content .= PHP_EOL . $emailFPay;

Writing to a new line in PHP

With your current code, check the page source and it is giving you the correct result.

But remember that you are running it on an html page so if you want a new line, use the <br> tag.

foreach ($gemList as $gem)
{
fwrite($file, $gem."<br>");
$i++;
}
fclose($file);

HTML does not take new lines \n into consideration, unless you specifically set the CSS property white-space:pre;

Unexpected PHP line breaks when writing to a file

What you have is linebreak characters that you can't see but that are being sent within the $_POST value, as mentioned by Marc B.

The work around to this is to remove the PHP End of Line Characters, which are handily referenced as PHP_EOL.

(Personally I would also reference array keys in single quotes as double quotes effects quote references and such within the array values)

So

$_POST['msg'] = str_replace(PHP_EOL, '', $_POST['msg']);
// str_replace($search,replace,subject);

This will remove system used line breaks from the string, before you can then save to the file. You can additionally replace standard line break [type character]s such as \n and \r (\r is not actually a linebreak as such, exactly) in case they're different from the PHP_EOL value :

Also suggested you save the changes as another variable rather than the original POSTed data.

$breaks = array("\n","\r",PHP_EOL);
$strippedMessage = str_replace($breaks, '', $_POST['msg']);

Writing TXT File with PHP, Want to Add an Actual Line Break

Sounds to me like you might be using single quotes, i.e. '\n' rather than "\n".

If you wanted to continue with a single quotes bias (as you should!), two options:

file_put_contents('/path/to/file.txt', 'Hello friend!
This will appear on a new line.
As will this');

// or

file_put_contents('/path/to/file.txt', 'Hello friend!'."\n".'This will appear on a new line.'."\n".'As will this');

PHP - how to create a newline character?

Only double quoted strings interpret the escape sequences \r and \n as '0x0D' and '0x0A' respectively, so you want:

"\r\n"

Single quoted strings, on the other hand, only know the escape sequences \\ and \'.

So unless you concatenate the single quoted string with a line break generated elsewhere (e. g., using double quoted string "\r\n" or using chr function chr(0x0D).chr(0x0A)), the only other way to have a line break within a single quoted string is to literally type it with your editor:

$s = 'some text before the line break
some text after';

Make sure to check your editor for its line break settings if you require some specific character sequence (\r\n for example).



Related Topics



Leave a reply



Submit