Home > Backend Development > PHP Tutorial > Detailed explanation of the problem when php handles line breaks

Detailed explanation of the problem when php handles line breaks

黄舟
Release: 2023-03-14 14:16:01
Original
1439 people have browsed it

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 "" at the end, which is "\n"; in Windows system, the end of each line is "", which is "\n\r"; in Mac system, The end of each line is "", 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" ;}
Copy after login

\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" ;
}
Copy after login


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=&#39;<br/>&#39;;
$newstr=str_replace($order,$replace,$str);
Copy after login


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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template