Line Break Not Working When Writing to Text File in PHP

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

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

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']);

PHP newline not working in text file

$numberNewline = $number . "\n";
fwrite($file, $numberNewline);

Try this

Output text file with line breaks in PHP

To convert the plain text line breaks to html line breaks, try this:

    $fh = fopen("filename.txt", 'r');

$pageText = fread($fh, 25000);

echo nl2br($pageText);

Note the nl2br function wrapping the text.

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 breaks not working in my php program?

You're saving to a text file, so it can't render HTML or JavaScript. Try saving with "html" if you want to see the line breaks.

<?php
$word = $_POST['word'];
$outPutString = $word.'<br>';

$fp = fopen('words.**html**', ab);
flock($fp, LOCK_EX);
fwrite($fp, htmlspecialchars($outPutString), strlen(outPutString));
flock($fp, LOCK_UN);
fclose($fp);
?>
<?php

$fo = fopen('words.**html**', rb);
flock($fo, LOCK_SH);
while(!feof($fo)) {
$words = fgets($fo);
if(!feof(fo)){
echo htmlspecialchars($words)."<br />";
}
}
flock($fo, LOCK_UN);
fclose($fo);

?>


Related Topics



Leave a reply



Submit