Php form conversion textarea line break Method

Source: Internet
Author: User
Tags php form

The following is my solution to this problem. At last, I fully understood it. It was a real risk of turning around the ship.

1.You must know that the linefeed in textarea is \ n (I found that the Enter key is \ n, as if it is \ r \ n in linux)

2.Before using nl2br, Please carefully read the manual explanation. I am depressed. Generally, it is understood that \ n is converted to <br>. Actually, it is not:

Refer to the php manual for explanation:

Nl2br-Inserts HTML line breaks before all newlines in a string

ReturnsStringWith '<br/>' inserted before all newlines

It means to Insert a new row before it <br/>

The w3cschool statement is as follows:

The nl2br () function inserts an HTML Line Break (<br/>) before each new line (\ n) in the string ).

Therefore, nl2br () can be inserted. <br> but \ n still exists. For example, the source code of the string is program \ n, we use programs after nl2br <br> \ n alive. So what we can see is:


Life network -------- line feed, because there is \ n in the source code

3. use the php function and str_replace to replace the function, such as str_replace ('\ n',' <br/> '). The problem is that the replacement is not successful and has never been replaced, after a long time, I even began to doubt whether the linefeed in textarea is \ n. Now I think it is too shaken, o (character _ character) o... In fact, after I replaced \ n/n separately, I intuitively told myself that I had entered a dead end. This is not a way to solve the problem, and there must be a fundamental error. Suddenly I thought about whether it would be a single or double quotation mark, so I changed str_replace ('\ n',' <br/> ') to str_replace ("\ n ", "<br/>. Great sweat!

I opened the manual and read the single quotation marks and double quotation marks again. I finally breathed a sigh of relief. I still have my own basic questions. php is easy to use and I still need to pay attention to the details.

The manual details the single double quotation marks: single quotation marks

The simplest way to specify a simple string is to use single quotes (Characters'.

To represent a single quotation mark, a backslash (\) Escape, same as many other languages. If a backslash is required before single quotes or at the end of a string, two backslashes are required.Note that if you try to escape any other character, the backslash itself will be displayed! Therefore, you do not need to escape the backslash itself.----- It seems a bit dizzy that the \ n in the string we replaced with str_replace ('\ n',' <br/> ') is not a line break. That is to say, the single quotation mark is a string, and php does not explain it. This is actually known when it is used elsewhere, but I did not expect that even line breaks will not be explained. Double quotation marks

If double quotation marks (") are used to enclose strings, PHP understands the escape sequence of more special characters:

Table 6-1. escape characters


\ N
Line feed (LF or ASCII characters 0 × 0A (10 ))

\ R
Press enter (CR or ASCII character 0 × 0D (13 ))

\ T
Horizontal tab (HT or ASCII character 0 × 09 (9 ))


\ $
Dollar sign

Double quotation marks

\ [0-7] {1, 3}
This regular expression matches a character in the octal symbol.

\ X [0-9A-Fa-f] {1, 2}
This regular expression matches a character in the hexadecimal notation.

In addition, if you try to escape any other characters, the backslash itself will be displayed!

---Now, the textarea line feed problem is clear, not line break or nl2br. It is because all transfers only exist in double quotation marks, and single quotation marks only process characters in php. What a depressing mistake. Remember it later.

You must know that the linefeed in textarea is \ n (I found that the Enter key is \ n, as if it is \ r \ n in linux)

This sentence has a problem: After my test, it is \ r \ n in windows, it is \ n in linux (this is not tested), and in win, \ r, \ n has the line feed function...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.