5

Morning,

When you post a form which has a textarea, what line endings are used? \r\n, \n or like local files does it depend on the OS?

I am tryring to find an RFC or W3C spec or something, or is it different based on OS/browser? Got a link?

eg

<textarea name="message">This is
my
message
</textarea>

Would it be

This is\r\n
my\r\n
message\r\n

or

This is\n
my\n
message\n

and is it a standard or different based on browser/os. Proof?

I am not worried about the textarea content before it's sent to the server, but the actual variable contents in PHP/server side.

Wizzard
  • 12,582
  • 22
  • 68
  • 101
  • When it stores in the DB, I need it to be consistant, so if a user on windows with IE saves something, it needs to have the same line endings as Linux with FireFox. I know locally there are different line endings, but when you submit a form the line endings on the textarea are the same I believe - I just can't find a spec/standard for this. – Wizzard Oct 13 '13 at 21:05
  • Just had a project and the content of the textarea in all browsers (IE7 to Chrome to FF and Safari) across all OS (Win XP - Win 8, Mac OS 10.5 - 10.9) behaved the same without doing something special. Using jQuery to submit asynchronously (I doubt there's some magic going on) and MySQL as DB engine with `utf8_unicode_ci` as column collation. We store the content without modification and write it back in a textarea on request. No problems up until now. ;-) – nietonfir Oct 13 '13 at 21:27

3 Answers3

8

This information can found in the HTML5 spec. It says that for the textarea element:

... there is the form submission value. It is normalized so that line breaks use U+000D CARRIAGE RETURN "CRLF" (U+000A) character pairs, and in addition, if necessary given the element's wrap attribute, additional line breaks are inserted to wrap the text at the given width.

The element's value is defined to be the element's raw value with the following transformation applied:

Replace every occurrence of a "CR" (U+000D) character not followed by a "LF" (U+000A) character, and every occurrence of a "LF" (U+000A) character not preceded by a "CR" (U+000D) character, by a two-character string consisting of a U+000D CARRIAGE RETURN "CRLF" (U+000A) character pair.

If the element's wrap attribute is in the Hard state, insert U+000D CARRIAGE RETURN "CRLF" (U+000A) character pairs into the string using a UA-defined algorithm so that each line has no more than character width characters. For the purposes of this requirement, lines are delimited by the start of the string, the end of the string, and U+000D CARRIAGE RETURN "CRLF" (U+000A) character pairs.

Alohci
  • 78,296
  • 16
  • 112
  • 156
  • You kinda mangled the crucial part of that quote. Here's the correct version: "It is normalized so that line breaks use U+000D CARRIAGE RETURN U+000A LINE FEED (CRLF) character pairs" – user98761 Jun 29 '20 at 19:43
  • @user98761 - No I didn't mangle it. That's [what it said](https://www.w3.org/TR/html50/forms.html#the-textarea-element). It's just the perils of linking to a living standard. I agree that what the standard says now is better worded. – Alohci Jun 29 '20 at 22:49
2

By HTML specs, a line break is represented as CR LF. In the default data format in form submission, it is represented encoded as %0D%0A. (This does not depend on the operating system and its native line ending conventions.) Your server-side code will take it from there. In PHP, you will see it as \r\n.

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
-1

you will receive plain text - if u need to add <br> tags you can use http://php.net/manual/en/function.nl2br.php =)

It's probably \n because when u dump it it return text line by line like when you use echo "something\nsomething";

miken32
  • 42,008
  • 16
  • 111
  • 154
Northys
  • 1,305
  • 3
  • 16
  • 32
  • Yes it will be plain text, but when it's stored in the DB will it be \n or \r\n ? Need something more than probably sorry :) – Wizzard Oct 13 '13 at 21:03