Method to convert textarea newline characters from a PHP form

  • 2020-03-31 21:16:48
  • OfStack

Here is how I solved the problem, and at last I came to a complete understanding

1. You must know that the newline in the textarea is \ nπ (personal detection found that pressing the enter key is \n, as if it were \r\n under Linux)

2. Before using nl2br, please read the manual carefully. I am just frustrated. Br > Not really:

See the PHP manual for explanation:

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

Returns a string with '< Br / > 'inserted before all newlines

Insert < before the new line. Br / >

W3cschool says:

Nl2br () function inserts the HTML newline character (<) before each new line (\n) in the string. Br / >) .

So nl2br() can be inserted < Br> But \n still exists, such as string source is: program \n life network, we nl2br after the program < Br> So the effect we will see is

The program

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

3. Replace str_replace(' \n','< Br / > '), the problem is that the replacement has not been successful, has not been replaced, I have been working for a long time and even began to doubt whether the newline character in the textarea is \n, now I think it is too shaky, o( studying _ studying )o...       I've actually tested it out by replacing       \ n            / n    A post-gut feeling told me that I was at a dead end, that this was not the way to solve the problem, that something was fundamentally wrong. All of a sudden I wondered if it could be single or double quotes, so I replaced str_replace(' \n','< Br / > ') into a str_replace (" \ n ", "< Br / >" (a light bulb went off and the replacement succeeded. Huge sweat!

I opened the manual again and read the single quotes and double quotes again. At last, I sighed. It was still my basic problem.

The manual's explanation of single and double quotes is detailed: Single quotes

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

To represent a single quote, escape with a backslash (\), as in many other languages. If you need a backslash before a single quote or at the end of a string, you need two backslashes. Note that if you try to escape any other characters, the backslash itself will be displayed! So you usually don't need to escape the backslash itself. So we use str_replace(' \n','< Br / > ') replaces the \n in the string instead of the newline character. That is to say, what's inside the single quote is a string, and PHP doesn't do anything to explain it, which is something that you know when you use it in other places, but it doesn't even explain line breaks. Double quotation marks

PHP knows more about escape sequences for special characters if you enclose strings with double quotes (") :

Table 6-1. Escape characters

The sequence
meaning

\ n
Line feed (LF or ASCII character 0 x 0A (10))

\ r
Enter (CR or ASCII character 0 x 0D (13))

\ t
Horizontal TAB (HT or ASCII character 0 x 09 (9))

\ \
The backslash

\ $
The dollar sign

\"
Double quotation marks

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

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

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

-- - Now, the textarea newline problem is clear, it's not a newline problem, it's not a nl2br problem, it's that all transitions are in double quotes, and single quotes are just characters in PHP. What a depressing mistake. Remember that in the future.

You must know that the newline character in the textarea is \n (personal detection found that pressing the enter key is \n, as if it were \r\n under Linux)

After my test, under Windows is \r\n, under Linux is \n(this is not measured), in addition, under win,\r,\n have the function of line wrap...

Related articles: