Home > Article > Backend Development > Detailed explanation of the problem when php handles line breaks
First let’s talk about the difference between \r and \n
The origin and difference between the two concepts of "Carriage Return" and "Line Feed".
Before computers appeared, there was something called a Teletype Model 33 (the concept of tty under Linux/Unix comes from this), which could type 10 characters per second. But there was a problem with it. It takes 0.2 seconds to complete a line break, which is enough to type two characters. If a new character is transmitted during this 0.2 seconds, then this character will be lost. I thought of a way to solve this problem, which is to add two characters to indicate the end of each line. One is called "carriage return", which tells the typewriter to position the print head at the left border; the other is called "line feed", which tells the typewriter to move the paper to the left. Move down one line. This is the origin of "line feed" and "carriage return", which can be seen from their English names.
Later, the computer was invented, and these two concepts were generally used. On computers. At that time, memory was very expensive, and some scientists thought it was too wasteful to add two characters at the end of each line. So, there was a disagreement. There is only "08ab8c7a5e8c3561646c85afea6ee002" at the end, which is "\n"; in Windows system, the end of each line is "08ab8c7a5e8c3561646c85afea6ee002ae50841a1e5ebedcd2097f8726239184", which is "\n\r"; in Mac system, The end of each line is "148abe006775c52e28d07bbe171fd752", that is, "\n"; A direct consequence is that if the file under Unix/Mac system is opened in Windows, all the text will become one line; while the file in Windows will If opened under Unix/Mac, there may be an extra ^M symbol at the end of each line.
When programming in c language (windows system)
\r is return to the beginning of the line. The previous output of this line will be overwritten.
For example: (Note that the following is C++ code)
In the end, only xixi will be displayed and hahaha will be overwritten.
int main (){cout << "hahaha" << "\r" << "xixi" ;}
int main() { cout << "hahaha" << "\n" << "xixi" ; }
The expression of the second line break character
In ordinary files such as .txt, .php, etc., the line breaks are "\r\n", "\n", "\r", but when displayed in HTML files (here is the explanation: HTML The line break in the TEXTAREA text field is also "\r" or "\n") is the "076402276aae5dbec7f672f8f4e5cc81" tag.
The code can be converted using PHP script. (Comes from PHP. Manual):
//Order of replacement $str="Line1\nLine2\rLine3\r\nLine4\n"; $order=array("\r\n","\n","\r"); $replace='<br/>'; $newstr=str_replace($order,$replace,$str);
The above is the detailed content of Detailed explanation of the problem when php handles line breaks. For more information, please follow other related articles on the PHP Chinese website!