php form conversion textarea newline method

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags debugging function it is key linux network network programming personal

Today in the project encountered a textarea wrap problem, debugging for a long time have not been resolved. To textarea in the carriage return line into br into the database.

Here is my solution to this problem, finally be completely understood, it is almost crevasse capsize

1. Must know that the newline character in textarea is n (personal test found that press the Enter key is n, as if Linux is rn)

2. Before using nl2br, carefully read the manual explained, I was depressing, generally understood as the n into a <br>, in fact, not:

PHP manual to explain:

nl2br & ndash; Inserts HTML line breaks before all newlines in a string

Returns string with '<br />' inserted before all newlines

This means inserting a new line before <br />

w3cschool's argument is:

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

So nl2br () can be inserted n <br> but still exists, for example, the source string is: n Life Network program, our program is <br> after nl2br n Life Network so we see the effect will be

program

Life Network -------- Wrap, because the source there are n

Replace the function with str_replace, such as str_replace ('n', '<br />') with the php function. The problem is that the substitution did not succeed, it has not been replaced, and I got it for a long time or even started to suspect a newline in the textarea Is not n, now think about it is too shaken, o (∩ _ ∩) o & nbsp; In fact, I tested after replacing n / n intuition told me that I came into a dead end, this is not the way to solve the problem, there certainly exist Fundamental error. Suddenly I thought of the problem of whether it would be single or double quotes, so I changed str_replace ('n', '<br />') to str_replace ("n", "<br />") Replaced by success. Khan!

Open the manual to single and double quotes and then read it again, and finally sigh, or their own basic problems, php with simple, or pay attention to the details ah.

Manual on the single and double quotes explanation is very detailed:

apostrophe

The easiest way to specify a simple string is to enclose it in single quotes (characters).

To represent a single quote, you need to escape it with a backslash (), as in many other languages. If you need a backslash before single quotes or at the end of a string, you need to use two backslashes. Note that if you try to escape any other character, the backslash itself will be displayed! So you usually do not need to escape the backslash itself.

----- So, instead of a newline, the string we replace with str_replace ('n', '<br />') seems a bit dizzy. In other words, within the single quotation marks is a string, php does not make any explanation, which is actually used when elsewhere know, but did not expect even a newline does not explain. Double quotes

If you enclose a string in double quotes ("), PHP understands more escaped sequences of special characters:

Table 6-1. Escape Characters

Sequence meaning

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

r
Enter (CR or ASCII characters 0 × 0D (13))

t
Horizontal tab (HT or ASCII characters 0 × 09 (9))

\
Backslash

$
Dollar sign

"
Double quotes

[0-7] {1,3}
This regular expression sequence matches a character in octal notation

x [0-9A-Fa-f] {1,2}
This regular expression sequence matches a character represented in hexadecimal notation

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

--- This next textarea wrap problem is clear, not a newline problem, nor is nl2br the problem is that all the transfer only exists in double quotes, single quotation marks in php only character processing. How depressing error ah. After some must keep in mind.

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: 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.