PHP Form Conversion TextArea The method of line break _php tips

Source: Internet
Author: User
Tags php form

Here is my solution to the problem, and finally I understand it completely, it's almost capsized in the gutter.

1. You must know that the line break in textarea is \ nthe (personal detection found to hit Enter is \ n, as if under Linux is \ r \ n)

2. before using the NL2BR, please carefully read the manual explanation, I just get depressed, generally understood as will be converted to <br, in fact, is not:

Read the PHP manual to explain:

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

Returnsstring with ‘<br />' inserted before all newlines

It means to insert <br before a new line/>

The W3cschool on the statement is:

The NL2BR () function inserts an HTML newline character (<br/>) before each new row (\ n) in the string.

So NL2BR () can insert <br> but still exist, such as string source code is: program \ Life Network, we nl2br after the program <br>\n Life Network so we see the effect will be

Program

The Life net ———————-change line, because the source code also has \ n

3. With PHP functions, str_replace replacement functions, such as str_replace (' \ n ', ' <br/> '), the problem is that the replacement did not succeed, has not been replaced, I have been for a long time even began to suspect that the textarea in the line break is not \ n,    Now think about it is too shaken, O (∩_∩) o ... In fact, I have to replace \n/n after the intuition tells me that I walked into a dead end, this is not the way to solve the problem, there must be some fundamental mistakes. Suddenly I thought about whether it would be a single or double quote, so I changed the Str_replace (' \ n ', ' <br/> ') to Str_replace ("\ n", "<br/>"), and the light flashed and the replacement succeeded. Big SWEAT!

Open the manual again to read the single and double quotes again, and finally heaved a sigh of relief, or their foundation of the problem Ah, PHP with simple, or to pay attention to the details AH.

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

Single quotation mark

The simplest way to specify a simple string is to use single quotes (characters‘)括起来。

To represent a single quote, you need to use a backslash (\)转义,和很多其它语言一样。如果在单引号之前或字符串结尾需要出现一个反斜线,需要用两个反斜线表示。注意如果你试图转义任何其它字符,反斜线本身也会被显示出来!所以通常不需要转义反斜线本身。

———— – so we have str_replace (' \ n ', ' <br/> ') replaced by \ n instead of line breaks, which seems a bit faint. That is, the single quotation mark is a string, PHP does not make any explanation, this is actually used in other places to know, but did not think that even line breaks are not explained.

Double quotes

If you enclose a string in double quotes ("), PHP knows more about the escape sequence for special characters:

table 6-1. Escape characters

Sequence
Meaning

\n
换行(LF 或 ASCII 字符 0×0A(10))

\r
回车(CR 或 ASCII 字符 0×0D(13))

\t
水平制表符(HT 或 ASCII 字符 0×09(9))

\\
反斜线

\$
美元符号

\”
双引号

\[0-7]{1,3}
此正则表达式序列匹配一个用八进制符号表示的字符

\x[0-9A-Fa-f]{1,2}
此正则表达式序列匹配一个用十六进制符号表示的字符

Also, if you try to escape any other character, the backslash itself is displayed!

——— This textarea the problem of line-wrapping is clear, not the problem of line breaks, nor the problem of NL2BR, is that all the transfer only exists in double quotes, single quotes in PHP only do character processing. What a depressing mistake. Be sure to remember it later.

You must know that the line break in textarea is \ nthe (personal detection found to hit Enter is \ n, as if under Linux is \ r \ n)

This is a problem: after my test, under Windows is \ r \ n, in Linux is \ n (this is not measured), in addition, in win, \r,\n have the function of line-wrapping ...

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.