What is the difference between " n" and " r n" (special characters for line breaks)?

Asked

Viewed 31,712 times

17

For line breaking, I usually use \n. But I realize that in some cases, such as in the Sublime Text editor, some texts that I need to capture the line-breaking are only captured when I use regex \r\n.

I’ve read some things on the internet about \r\n and the only thing I understood is that this has something to do with Windows Operating System.

In some cases, I had trouble using the \n in a file download system txt. This system is written in PHP. I needed to replace the line breaks \n for \r\n for the text to be interpreted correctly. But this left me in doubt whether the problem was related to the operating system or the PHP programming language.

So:

  • What’s the difference between \n and \r\n?

  • When I should wear one or the other?

  • This is related to the operating system I use, or the programming language (as in the PHP example)?

  • \r = Return and \n = newline, if this developing php is use nl2br and follow http://php.net/manual/en/function.nl2br.php ... depends on what you need.

  • Thank you @Kingrider. Actually the problem isn’t even PHP, I just made a siting. Note that I didn’t add the [tag:php]. That’s because I really want to understand the meaning, when I should use, among other things.

  • 2

    Related: http://answall.com/questions/93444/qual-a-diff%C3%a7a-entre-Carriage-Return-e-line-feed

2 answers

25


The \n means "new line" or "line-feed", or "new line". The \r means "Carriage Return", or "car return". When the ASCII table was standardized, the \n received code 10 and \r received the code 13.

The idea originally, when character encoding tables as bit sequences were designed, is that the \n was interpreted as the command to make the cursor move down, and the \r the command for it to move back to the beginning of the line. This distinction was important for digital typewriters that preceded computers, for digital telegraphs, for teletypes, and for the programming of the first printers that emerged. In fact, this is surprisingly older than you think, already appearing in the year 1901 along with some of the first of these character encoding tables.

Therefore, in a text for a line break to be inserted, it was necessary to use \r\n. First the cursor should move to the beginning of the line and then down. And that was the line-breaking pattern adopted much later by Microsoft.

Multics (and later Unix) followed a different path, and decided to implement the \n as a line-breaker, which already included a return car. After all, it doesn’t make much sense to have one thing without having the other together, and using them as one thing ensures that they will never be separated. This also has the advantage of saving space by using a single byte to encode the line break instead of two, and in those years where memory was small and low power processing, every byte saved counted for a lot.

Other companies, such as Apple and Commodore, also followed a similar path to Unix, but instead adopted the \n for line breaks, adopted the \r.

Other smaller companies have adopted other codes for line breaking. For example, the QNX adopted character 30 from the ASCII table. Atari adopted 155. Acorn and RISC OS adopted the \n\r instead of \r\n. Sinclair adopted the 118.

Sources:

8

According to the same topic in English:

\r = CR (Carriage Return) // Used as line break on Mac OS prior to version X

\n = LF (Line Feed) // Used as line break Unix/Mac OS superior to X version

\r n = CR + LF // Used as line break in Windows

When to use each?

File generation

In a log file recording, for example, that will be executed/processed by another application, it is important to follow the default environment, since the target application will also do so. Doing this is recommended only if you are writing code for a specific environment. PHP has a reserved constant PHP_EOL (strongly recommend), which will solve the line break for you according to the environment, so you will make your cross-platform code, regarding the line break.

HTML

Sending the content (text) of a file to be rendered by the browser, it is interesting to use the function nl2br which will convert all line breaks into <br />.

  • 2

    What is the difference? When should I use? The operating system part is giving to understand, although the answer does not speak directly.

  • 1

    This will depend on your goal and the operating system. For example, if you are printing an html for the browser, it would be good to use the function nl2br of PHP. In the case of saving a log file, which will be processed by another resource for example, consider the default target OS, since applications, libraries, etc, will handle your file in the default environment.

  • 1

    I think this could be in your answer then. I think it will get better. So I think users will remove the negatives.

  • I complemented the answer. I hope I helped.

  • Thank you, young man. That’s good :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.