Analyze the problem of PHP processing newlines rn_PHP tutorial

WBOY
Release: 2016-07-21 15:06:26
Original
1153 people have browsed it

First, let’s talk about the difference between r and n
The origin and difference of the two concepts "Carriage Return" and "Line Feed".
Before the advent of computers, there was something called a Teletype Model 33 (the concept of tty in Linux/Unix comes from this), which could type 10 characters per second. But it had a problem. It takes 0.2 seconds to type a new line, which is enough to type two characters. If a new character is transmitted during this 0.2 seconds, then this character will be lost. The staff came up with a way to solve this problem, which was to add two end characters after 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. 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 also used. Generally on computers. At that time, memory was very expensive, and some scientists thought that adding two characters at the end of each line was too wasteful. So, there was a disagreement. The end of the line is only "", which is "n"; in the Windows system, the end of each line is "", which is "nr"; in the Mac system, the end of each line is It is "", that is, "n";. A direct consequence is that if the file under the Unix/Mac system is opened in Windows, all the text will become one line; while the file in Windows will be opened in Unix/Mac. If you open it below, 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 this line. This will return this line. The previous output is overwritten.

For example: (Note that the following is C++ code)

Only xixi is displayed in the end, and hahaha is overwritten.

Copy code.

The code is as follows: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.


Copy code
The code is as follows:int main(){
cout << "hahaha" << "n" < "xixi" ; .php, etc.) newline characters are "rn", "n", "r". But when it is displayed in an HTML file (note here: the line break in the TEXTAREA text field of HTML is also "r" or "n") it is the "
" tag.
Conversion can be done using PHP script.

The code is as follows (from the PHP manual):



Copy the code The code is as follows:
//Order of replacement
$str="Line1nLine2rLine3rnLine4n";
$order=array("rn","n","r");$replace='
' ;
$newstr=str_replace($order,$replace,$str);
http://www.bkjia.com/PHPjc/327628.html
www.bkjia.com

true

http: //www.bkjia.com/PHPjc/327628.html

TechArticle
First, let’s talk about the difference between r and n” (Carriage Return) and “Line Feed” (Line Feed) The origin and difference between these two concepts. Before computers appeared, there was something called...

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!