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
How Does Pcntl_Fork Work in PHP
A _Construct on an Eloquent Laravel Model
Fix iOS Picture Orientation After Upload PHP
Php/Gd - Transparent Background
Using the Browser Prompt to Download a File
Connecting to Websocket with PHP Client
PHP Variable Scope Between Code Blocks
How to Determine Whether It's a Mobile Device with PHP
[PHP Warning: Mail(): " Sendmail_From" Not Set in PHP.Ini or Custom "From:" Header Missing
When to Wrap Curly Braces Around a Variable
How Download Big File Using PHP (Low Memory Usage)
MySQL Binary Storage Using Blob VS Os File System: Large Files, Large Quantities, Large Problems
Add Column to Magento Admin Catolog > Manage Products
How to Upload Multiple Image in Laravel
Parsing Http_Range Header in PHP
Apache's Errordocument Directive Does Not Redirect
Compile Error: "G++: Error Trying to Exec 'Cc1Plus': Execvp: No Such File or Directory"