How to convert textarea line breaks in PHP forms

Source: Internet
Author: User
To convert the carriage return in textarea to BR into the database, that is, to convert the line break in textarea, debugging for a long time. 1. You must know that the line break in TextArea is \ n (personal detection found that pressing ENTER is \ n, as if it were \ r \ n under Linux)

To convert the carriage return in textarea to BR into the database, that is, to convert the line break in textarea, debugging for a long time.

1. You must know that the line break in TextArea is \ n (personal detection found that pressing ENTER is \ n, as if it were \ r \ n under Linux)

2. Before using the NL2BR, please carefully watch the manual explanation, I was depressed, generally understood to convert \ n to
, not really:

Read the PHP manual to explain:

Nl2br–inserts HTML line breaks before all newlines in a string

Returns string with '
' Inserted before all newlines

It means inserting before a new line.

W3cschool on the word:

The NL2BR () function inserts an HTML newline character before each new line (\ n) in the string (
)。

So NL2BR () can insert
But \ n still exist, for example, string source code is: program \ \ Live NET, we nl2br after the program
\ nthe life net so the effect we see will be

Program

Life net ———————-to change, because the source also has \ n

3. Using PHP functions, str_replace Replace functions, such as str_replace (' \ n ', '
'), the problem is that the replacement has not been successful, has not been replaced, I have been a long time even began to suspect textarea in the line is not \ n, now think is really too shaken, O (∩_∩) o ... In fact, I passed the test to replace \n/n after the intuition told me that I walked into a dead end, this is not the way to solve the problem, there must be a fundamental error. Suddenly I thought of the question of whether it would be single or double quotes, so str_replace (' \ n ', '
Change to Str_replace ("\ n", "
"), the replacement succeeds.

The explanations in the manual for single and double quotes are detailed:

The simplest way to specify a simple string is to enclose it in single quotation marks (characters ').

To represent a single quotation mark, you need to escape with a backslash (\), as in many other languages. If a backslash is required before the single quotation mark or at the end of the string, it needs to be represented by two backslashes. Note that if you try to escape any other characters, the backslash itself will be displayed as well! Therefore, it is usually not necessary to escape the backslash itself.

If you enclose strings in double quotation marks ("), PHP knows more about escape sequences for special characters:

Table 6-1. Escape character

Sequence meaning

\ n line break (LF or ASCII characters 0x0a (10))

\ r Enter (CR or ASCII character 0x0d (13))

\ t Horizontal tab (HT or ASCII character 0x09 (9))

\ \ Backslash

\$ dollar Sign

\ "Double quotation marks

\[0-7]{1,3} This regular expression sequence matches a character represented by an octal symbol

\x[0-9a-fa-f]{1,2} This regular expression sequence matches a character that is represented by a hexadecimal symbol

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

TextArea line break is not a question of line breaks, nor is it a matter of nl2br, that all transfers only exist in double quotes, and single quotes do only character processing in PHP.

>>> you may be interested in the article: Php Stripped string line break instance parsing php compressed HTML (clear line breaks, clear tabs, remove comment marks) PHP regular filter HTML tags, spaces, Code examples such as line breaks PHP methods for removing line breaks summarize PHP compressed HTML page code (clear spaces, line breaks, tabs, comment markers, etc.) methods to provide several PHP replacement line breaks PHP generates Excel and controls line breaks in Excel cells

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