The HTTP and MIME specs specify that header lines must end with \r\n, but they aren't clear (some would argue that it isn't clear if they are clear) about what to do with the contents of a TEXTAREA. (See, for instance, this thread from an HTML working group about the issue.)
Here's a quote from the HTTP/1.1 spec about message headers:
The line terminator for message-header fields is the sequence CRLF. However, we recommend that applications, when parsing such headers, recognize a single LF as a line terminator and ignore the leading CR.
I think that is a good strategy in general: be strict about what you produce but liberal in what you accept. You should assume that you will receive all sorts of line terminators. (Note that in addition to CRLF and LF, Mac OS-9 used CR alone, and there are still a few of those around. The Unicode standard (section 5.8) specifies a wide range of character sequences that should be recognized as line terminators; there's a list of them here.)
Answer from Ted Hopp on Stack OverflowThe HTTP and MIME specs specify that header lines must end with \r\n, but they aren't clear (some would argue that it isn't clear if they are clear) about what to do with the contents of a TEXTAREA. (See, for instance, this thread from an HTML working group about the issue.)
Here's a quote from the HTTP/1.1 spec about message headers:
The line terminator for message-header fields is the sequence CRLF. However, we recommend that applications, when parsing such headers, recognize a single LF as a line terminator and ignore the leading CR.
I think that is a good strategy in general: be strict about what you produce but liberal in what you accept. You should assume that you will receive all sorts of line terminators. (Note that in addition to CRLF and LF, Mac OS-9 used CR alone, and there are still a few of those around. The Unicode standard (section 5.8) specifies a wide range of character sequences that should be recognized as line terminators; there's a list of them here.)
what do browsers send up for a
<textarea></textarea>if it has multiple lines?
All modern browsers send CRLF (\r\n). However this is not something that has been satisfactorily standardised so I would definitely consider it worthwhile to normalise the newlines of all multi-line input text.
When the value is read through JavaScript rather than being submitted directly from a form, browser behaviour differs. IE and Opera return strings with CRLFs in; Firefox and WebKit return LF. So any form that gets submitted with JavaScript/XMLHttpRequest help is likely to come in either form.
If you cannot change the code, it is more complicated but still possible.
The browser is going to filter/encode/sanitize everything you enter in the text field, so you will have to do something after that step.
My suggestion is:
- use Firefox
- install extension TamperData
- use it to modify the text you send, e.g. inserting
%0D%0Aat the beginning; which is the URL-encoded version of\r\n
A little bit cumbersome but doable.
<input type="*"> doesn't accept newlines.
Use <textarea rows="1"></textarea> instead.
Bear in mind that you will have to show your output in a <pre> or another <textarea> if you want to show the newlines. Other possibility is using PHP's nl2br().
A new line inside the html source code is not displayed as a newline in a browser.
Take a look at the source code of your generated html pages, you will find your newlines
The html code of a new line is <br>
In short
<?php echo "\r\n"; ?> // Writes a new line in the output stream
<?php echo "<br>"; ?> // Writes the newline html code
Another example:
<php
echo "<html><body>A<br>\r\nB<br>C\r\n</body></html>"
?>
Will output the raw text:
<html><body>A<br>
B<br>C
</body></html>
A browser will display this
A
B
C
AFAIK, lines break are made with <br> or <br /> tags.
You can convert \n to <br /> with nl2br function:
<?php
$line = "First line\nSecond line."
echo nl2br($line);
?>
\r is "Carriage Return" (CR, ASCII character 13), \n is "Line Feed" (LF, ASCII character 10). Back in the days, you had two ASCII characters at the end of each line to tell a printer what to do - CR would tell the printer to go back to the left edge of the paper, LF would advance to the next line.
Operating systems still have different conventions as to what the end of a line looks like -- some of them have \n\r, some have \n, some have \r\n.
In Javascript, you mostly deal with \n - this is how strings are typically switching to the next line. However, depending on what strings you are working with, you may be encountering \r as well.
Normally \r represents a carriage return character (ASCII 0x0d), and \n is a newline character (ASCII 0x0a). This page has a list of all the special characters, quoted here for completeness:
\fmatches form-feed.\rmatches carriage return.\nmatches linefeed.\tmatches horizontal tab.\vmatches vertical tab.\0matchesNULcharacter.[\b]matches backspace.\smatches whitespace (short for[\f\n\r\t\v\u00A0\u2028\u2029]).\Smatches anything but a whitespace (short for[^\f\n\r\t\v\u00A0\u2028\u2029]).\wmatches any alphanumerical character (word characters) including underscore (short for[a-zA-Z0-9_]).\Wmatches any non-word characters (short for[^a-zA-Z0-9_]).\dmatches any digit (short for[0-9]).\Dmatches any non-digit (short for[^0-9]).\bmatches a word boundary (the position between a word and a space).\Bmatches a non-word boundary (short for[^\b]).\cXmatches a control character. E.g:\cmmatchescontrol-M.\xhhmatches the character with two characters of hexadecimal codehh.\uhhhhmatches the Unicode character with four characters of hexadecimal codehhhh.
» npm install react-native-render-html
This is to show new line and return carriage in HTML. Then you don't need to do it explicitly. You can do it in CSS by setting the white-space attribute pre-line value.
<span style="white-space: pre-line">@Model.CommentText</span>
You can use CSS white-space property for \n. You can also preserve the tabs as in \t.
For line break \n:
white-space: pre-line;
For line break \n and tabs \t:
white-space: pre-wrap;
document.getElementById('just-line-break').innerHTML = 'Testing 1\nTesting 2\n\tNo tab';
document.getElementById('line-break-and-tab').innerHTML = 'Testing 1\nTesting 2\n\tWith tab';
#just-line-break {
white-space: pre-line;
}
#line-break-and-tab {
white-space: pre-wrap;
}
<div id="just-line-break"></div>
<br/>
<div id="line-break-and-tab"></div>
\r\n is a Windows Style
\n is a POSIX Style
\r is a old pre-OS X Macs Style, Modern Mac's using POSIX Style.
\r is a carriage return and \n is a line feed. On old computers without monitors, you needed to use paper and a printer to get a program's result to the user. In order for new lines in your printed file to start at the leftmost column, files needed both a \n for line Feed, and a \r for get carriage return to the most left position. This is a carryover from the computers of yesteryear, and is generally now only seen on Windows.
\n means new line. It means that the cursor must go to the next line.
\r means carriage return. It means that the cursor should go back to the beginning of the line.
Unix programs usually only needs a new line (\n).
Windows programs usually need both.