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 "
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" ;}
\n is carriage return + line feed. Move the cursor to the beginning of the line first and then change to the next line, which is the beginning of the next line. 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 "
" 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!