Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP echo the equivalent of CR LF of Notepad

Tags:

php

encoding

I need to export some data using PHP and for each line I'm adding a \r\n. When I open the exported data file that I downloaded, I see that the \r\n is interpreted as [LF] in Notepad.

But the application in which I open the file doesn't read the [LF] as a new line.

Now if I do a [CR][LF] in Notepad, the application can read the [CR][LF] as a new line.

How can I echo the equivalent of [CR][LF] with PHP?

like image 223
troc Avatar asked Jun 27 '12 08:06

troc


People also ask

What is PHP CRLF?

If you need to insert a Carriage Return in a string in PHP: Carriage return is: "\r" But carriage return (CR) is different from "new line" (NL) New Line (NL) is "\n" What in HTML is called "line break" is the sum of a CR and a NL.

What is CRLF characters?

The CRLF abbreviation refers to Carriage Return and Line Feed. CR and LF are special characters (ASCII 13 and 10 respectively, also referred to as \r\n) that are used to signify the End of Line (EOL).

What is CRLF in ASCII?

Description. The term CRLF refers to Carriage Return (ASCII 13, \r ) Line Feed (ASCII 10, \n ). They're used to note the termination of a line, however, dealt with differently in today's popular Operating Systems.


2 Answers

It's as simple as doing:

echo "\r\n";

(note the double quotes)

like image 91
Mark Baker Avatar answered Oct 21 '22 00:10

Mark Baker


Do echo PHP_EOL;

That way it'll always display the correct linefeed/carriage return combination that's valid for the system you're on, since not all OSes use the same newline convention. (More information: PHP global constants.)

like image 24
Harald Brinkhof Avatar answered Oct 21 '22 01:10

Harald Brinkhof