62

I've seen many developers use different methods to split a string by new lines, but i'm confused which is the correct: \r\n OR \n only?

CodeOverload
  • 47,274
  • 54
  • 131
  • 219
  • 1
    This really depends on the format of the file/string your are processing. – MrGlass Jan 28 '11 at 03:41
  • 2
    There's a PHP_EOL constant available if you want to embed the native system eol character(s) into your strings. Doesn't help much if you're dealing with text that came from other systems, though. – Marc B Jan 28 '11 at 03:43
  • You might find [`s($str)->normalizeLineEndings()`](https://github.com/delight-im/PHP-Str/blob/8fd0c608d5496d43adaa899642c1cce047e076dc/src/Str.php#L540) helpful, as found in [this standalone library](https://github.com/delight-im/PHP-Str). This turns *all* newlines into `\n`, even the Unicode newlines that this question didn't mention. – caw Jul 27 '16 at 02:44

4 Answers4

99

\n is used for Unix systems (including Linux, and OSX).

\r\n is mainly used on Windows.

\r is used on really old Macs.

PHP_EOL constant is used instead of these characters for portability between platforms.

Ali Raza
  • 794
  • 7
  • 12
icktoofay
  • 126,289
  • 21
  • 250
  • 231
  • 1
    \r is sometimes used on current macs too, depending on the text editor. Most plain text editors have switched to unix style, though. – ughoavgfhw Jan 28 '11 at 03:42
  • Quite right. To be more specific for those who don't know, "most UNIX systems" that use `\n` includes Mac OS X. – Wiseguy Jan 28 '11 at 03:42
  • 4
    Just to be clear: really old Macs here mean programs coded for OS 9, which was declared dead on 2002. – Yuji Jan 28 '11 at 03:43
  • In many languages, such as C, "\n" is used in text-mode files, even on platforms which use "\r\n". Are you sure that in PHP "\r\n" is used internally, or is "\n" sufficient? (I don't know.) – mgiuca Jan 28 '11 at 03:44
  • I've used \r\n to split a string from MySQL, but it works with only \n if it's a php string, wiered – CodeOverload Jan 28 '11 at 03:44
  • 9
    For a little historical perspective, there are from the days when printers only printed characters (like a typewriter). \r is a carriage return (literally, move the printing head back to the beginning of the line), and \n is newline (move the paper up). More info here: http://en.wikipedia.org/wiki/Carriage_return – Chris Shain Jan 28 '11 at 03:44
  • @David Probably that's due to what I was talking about in my comment. Certainly in C it is just ``\n``. – mgiuca Jan 28 '11 at 03:45
  • 3
    With further regards to the historic aspect, the reason there are two characters in order to print a "full" newline was because it took time for the print head to return to the start of the line, so just having another character that didn't require the print head itself to do anything allowed the whole system to work efficiently without any futzing about with the client having to wait and then resync after sending a newline. – Anon. Jan 28 '11 at 03:49
34

The given answer is far from complete. In fact, it is so far from complete that it tends to lead the reader to believe that this answer is OS dependent when it isn't. It also isn't something which is programming language dependent (as some commentators have suggested). I'm going to add more information in order to make this more clear. First, lets give the list of current new line variations (as in, what they've been since 1999):

  • \r\n is only used on Windows Notepad (fixed as of 2018), the DOS command line (PowerShell handles \n only just fine; as well as most modern versions of DOS-era command line applications [such as more]), most of the Windows API written before ~2000, and some (older) Windows apps (mostly because they use the Windows API).
  • YMMV for .ps1, .bat, and .cmd scripts.
  • Oddly enough, TCP/IP uses \r\n. This means that most web protocols (including HTTP itself) use \r\n as noted by Raatje. There are definitely times that this matters for you as a PHP programmer.
  • \n is used for all other systems, applications and the content of webpages/email.

You'll notice that I've put most Windows apps in the \n group which may be slightly controversial but before you disagree with this statement, please grab a UNIX formatted text file and try it in 10 web friendly Windows applications of your choice (which aren't listed in my exceptions above). What percentage of them handled it just fine? You'll find that they (practically) all implement auto detection of line endings or just use \n because, while Windows may use \r\n, the Internet and most other OSes just use \n. Therefore, it is best practice for applications to use \n alone if you want your output to be Internet friendly.

PHP also defines a newline character called PHP_EOL. This constant is set to the OS specific newline string for the machine PHP is running on (\r\n for Windows and \n for everything else). This constant is not very useful for webpages and should be avoided for HTML output or for writing most text to files. It becomes VERY useful when we move to command line output from PHP applications because it will allow your application to output to a terminal Window in a consistent manner across all supported OSes.

If you want your PHP applications to work from any server they are placed on, the two biggest things to remember are that you should always just use \n unless it is terminal output (in which case you use PHP_EOL) and you should also ALWAYS use / for your path separator (not \). The third thing to look out for is drive letters in path strings, if allowed, which may be tricky depending on what you're doing.

The even longer explanation:

An application may choose to use whatever line endings it likes regardless of the default OS line ending style. If I want my text editor to print a newline every time it encounters a period that is no harder than using the \n to represent a newline because I'm interpreting the text as I display it anyway. IOW, I'm fiddling around with measuring the width of each character so it knows where to display the next so it is very simple to add a statement saying that if the current char is a period then perform a newline action (or if it is a \n then display a period).

Aside from the null terminator, no character code is sacred and when you write a text editor or viewer you are in charge of translating the bits in your file into glyphs (or carriage returns) on the screen. The only thing that distinguishes a control character such as the newline from other characters is that most font sets don't include them (meaning they don't have a visual representation available).

That being said, if you are working at a higher level of abstraction then you probably aren't making your own textbox controls. If this is the case then you're stuck with whatever line ending that control makes available to you. Even in this case it is a simple (and fairly quick) matter to automatically detect the line ending style of any string and adjust accordingly.

krowe
  • 2,129
  • 17
  • 19
  • 1
    "you should always just use `\n` unless it is terminal output" - I suspected this, but this clarity on the reasoning is just what I was looking for. – sfarbota Sep 11 '15 at 13:14
  • 3
    You are way off with the statement "\n is used for all other systems, applications and the Internet." Many Internet protocols require the \r\n line endings Http headers are required to be terminated with \r\n and the headers and message is to be seperated with \r\n – Raatje Feb 26 '17 at 08:10
  • 4
    See also HTTP (7230), SMTP (RFC5321), POP3 (RFC1939) and email (RFC2822) which state MUST for \r\n line endings – Raatje Feb 26 '17 at 08:40
  • 1
    The CSV spec also requires a \r\n ending https://tools.ietf.org/html/rfc4180#page-2 – Clement Jul 09 '20 at 08:20
  • Do note that Notepad since 2018 supports \n file endings. https://devblogs.microsoft.com/commandline/extended-eol-in-notepad/ – pratikpc Mar 08 '22 at 06:29
  • @Raatje Sorry that it took so long for me to add your information. Of coarse, you are right. It has been fixed. – krowe Mar 16 '22 at 18:27
  • @Clement Technically, you are not wrong. Practically, every application that I can think of which can read CSV data can handle either line ending automatically. – krowe Mar 16 '22 at 18:41
5

If you are programming in PHP, it is useful to split lines by \n and then trim() each line (provided you don't care about whitespace) to give you a "clean" line regardless.

foreach($line in explode("\n", $data))
{
    $line = trim($line);
    ...
}
gahooa
  • 131,293
  • 12
  • 98
  • 101
1

For php, \n should work for you!

http://php.net/manual/en/language.types.string.php

D.R.
  • 1,199
  • 5
  • 19
  • 42