Php form conversion textarea line break method

Source: Internet
Author: User
Tags php form
Today, I encountered a textarea line feed problem in the project, which was not solved after debugging for half a day. You need to convert the carriage return text in textarea into a br and store it in the database. 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 and generally understand it as converting \ n
Actually not:

Refer to the php Manual for explanation:

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

ReturnsStringWith'
'Inserted before all newlines

It means to insert a row before the new row.

The w3cschool statement is as follows:

The nl2br () function inserts an HTML line break (
).

Therefore, nl2br () can be inserted.
But \ n still exists. for example, the source code of the string is: Program Life Network. after nl2br, the program
\ N Life Network, so what we can see is

Program

Life Network -------- line feed, because the source code also contains

3. replace the function with the php function and str_replace, such as str_replace ('','
') The problem is that the replacement was not successful and never been replaced. after a long time, I began to doubt whether the linefeed in textarea was \ n. now I think it is too shaken, o (distinct _ distinct) 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 quotation mark or double quotation mark, so the str_replace ('','
') Changed to str_replace ("","
"), The light flashed, and the replacement was successful. 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.----- So we use str_replace ('','
') It seems that the \ n in the replaced string 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

Sequence
Description

\ 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 ))

\\
Backslash

\ $
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...

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: info-contact@alibabacloud.com 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.