PHP form Conversion TextArea line break Method _php Tutorial

Source: Internet
Author: User
Tags php form
Here is my solution to the problem, and finally I understand it, it's a sewer that almost capsized

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 with string '
' 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


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 spark flashed, the substitution succeeded. Big SWEAT!

Open the manual and double quotation marks again to see again, finally heaved a breath, or their own basic problem ah, PHP with simple, or to pay attention to the details AH.

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

Single quotation marks

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.

———— – so we use Str_replace (' \ n ', '
') to replace \ n instead of a newline character in the string, which appears to be somewhat faint. That is, the single quotation mark is a string, PHP does not make any explanation, this is in fact used in other places to know, but did not think that even the line break is not explained.

Double quotes

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

table 6-1. Escape characters


NewLine (LF or ASCII characters 0x0a (10))

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

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

Reverse Slash

Dollar sign

Double quotes

This regular expression sequence matches a character represented by an octal symbol

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!

——— This textarea the problem of line break is clear, not the problem of newline, nor the problem of NL2BR, is that all the transfer only exists in double quotes, single quotation marks in PHP only do character processing. What a depressing mistake. Must remember in the future.

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)

This sentence has the question: after my test, under Windows is \ r \ n, in Linux is \ n (this is not measured), in addition, under win, \r,\n have a line-wrapping function ... true techarticle here is my solution to this problem, and finally I understand it, it was almost overturned in the gutter 1. You must know that the line break in TextArea is \ n (Personal detection found press ENTER ...)

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