Convert the carriage return of the text submitted by textarea to <p>

Source: Internet
Author: User
The carriage return of the text submitted by the form textarea is converted to the text submitted in the textarea of the & lt; p & gt; form. after the text is submitted, it is written to the mysql database, then read and display it. The result shows that the carriage return is missing. There are many sections, and the results are combined into a large segment.

Is there any way to convert the carriage return submitted in the form textarea into an image?

What about this html tag? What is the conversion before writing data to the database? Or can it be converted after being read from the database?

Can you give me a specific code?


Reply to discussion (solution)

I found the answer on the Internet, that is, when saving it to the database, it is not processed. Use this statement to read the data:

CurrentContent = conn. myReader ["Content"]. ToString (). Replace (Convert. ToString (char) 13 ),"
");

But this is an object-oriented method that I don't quite understand. What are process-oriented statements translated?

Do you want php code or code?

Do you want php code or code?


Is the php code.

My own program is to use the $ select = mysql_query ("select...") statement to query a result.
The following code is used to retrieve data:
While ($ arr = mysql_fetch_array ($ result ))
{

}
The field I need is in $ arr [txt], and how can I process it to convert the carriage return
?

Echo nl2br ($ arr [txt]);

Http://blog.csdn.net/beyondqqiang/article/details/8893663
This should help you.

Http://blog.csdn.net/beyondqqiang/article/details/8893663
This should help you.


Thank you for your information. this is complicated. I have never learned any regular expressions. In addition, the function is also complex, that is, to convert multiple-line carriage return into


My requirement is very simple, that is, converting a carriage return submitted in textarea into
Can you help me write the code? It is said that it remains unchanged when stored in the database. The read statement is as follows:
$ Select = mysql_query ("select ....")
While ($ arr = mysql_fetch_array ($ result ))
{
... Processing statement
}
The field I need is in $ arr [txt], and how can I process it to convert the carriage return
?

With nl2br ($ arr [txt]), there is still no line break.

I wrote one myself:
Str_replace (char (13 )),"
", $ Arr [txt]);

As if there is a syntax problem, opening the page is blank. (This implies that some php statements have syntax errors .)

$ Arr ['txt '] = nl2br ($ arr ['txt']);

Str_replace (char (13 )),"
", $ Arr [txt]);
Yes, because php does not have the char Function and only has the chr function.
However, it is inappropriate to write chr (13), because the linux text does not have this "carriage return" character, so if you write it yourself, you should write it.
$ Arr ['txt '] = preg_replace ("/[\ r \ n] + /","
", $ Arr ['txt ']);

$ Arr ['txt '] = nl2br ($ arr ['txt']);

Str_replace (char (13 )),"
", $ Arr [txt]);
Yes, because php does not have the char Function and only has the chr function.
However, it is inappropriate to write chr (13), because the linux text does not have this "carriage return" character, so if you write it yourself, you should write it.
$ Arr ['txt '] = preg_replace ("/[\ r \ n] + /","
", $ Arr ['txt ']);


Thank you! You can use this statement, although I don't quite understand what it means. The main reason is that the first parameter is not quite understandable and you will learn it later.

Unless your $ arr ['txt '] does not contain text line breaks, the nl2br will not be used.

$ Arr ['txt '] = nl2br ($ arr ['txt']);

Str_replace (char (13 )),"
", $ Arr [txt]);
Yes, because php does not have the char Function and only has the chr function.
However, it is inappropriate to write chr (13), because the linux text does not have this "carriage return" character, so if you write it yourself, you should write it.
$ Arr ['txt '] = preg_replace ("/[\ r \ n] + /","
", $ Arr ['txt ']);


Can you help me write another statement that converts spaces in textarea to & nbsp? I have not learned these expressions yet.


$ Arr ['txt '] = nl2br ($ arr ['txt']);

Str_replace (char (13 )),"
", $ Arr [txt]);
Yes, because php does not have the char Function and only has the chr function.
However, it is inappropriate to write chr (13), because the linux text does not have this "carriage return" character, so if you write it yourself, you should write it.
$ Arr ['txt '] = preg_replace ("/[\ r \ n] + /","
", $ Arr ['txt ']);


Thank you! You can use this statement, although I don't quite understand what it means. The main reason is that the first parameter is not quite understandable and you will learn it later.



This "/[\ r \ n] +/" is the line break symbol in the file, but the nl2br function can be used to convert the line break symbol entered in textarea
Because the newline character in the text field is "\ n", not "\ r \ n ". The nl2br function is ineffective. maybe you didn't press enter in the text field.

That is regular



$ Arr ['txt '] = nl2br ($ arr ['txt']);

Str_replace (char (13 )),"
", $ Arr [txt]);
Yes, because php does not have the char Function and only has the chr function.
However, it is inappropriate to write chr (13), because the linux text does not have this "carriage return" character, so if you write it yourself, you should write it.
$ Arr ['txt '] = preg_replace ("/[\ r \ n] + /","
", $ Arr ['txt ']);


Thank you! You can use this statement, although I don't quite understand what it means. The main reason is that the first parameter is not quite understandable and you will learn it later.



This "/[\ r \ n] +/" is the line break symbol in the file, but the nl2br function can be used to convert the line break symbol entered in textarea
Because the newline character in the text field is "\ n", not "\ r \ n ". The nl2br function is ineffective. maybe you didn't press enter in the text field.


Press enter, but no line break is displayed.

Unless your $ arr ['txt '] does not contain text line breaks, the nl2br will not be used.


Excuse me, the nl2br function converts the values 0X0 D and 0x0 A (hexadecimal carriage return)
Why?

$ Arr ['txt '] = nl2br ($ arr ['txt']);

Str_replace (char (13 )),"
", $ Arr [txt]);
Yes, because php does not have the char Function and only has the chr function.
However, it is inappropriate to write chr (13), because the linux text does not have this "carriage return" character, so if you write it yourself, you should write it.
$ Arr ['txt '] = preg_replace ("/[\ r \ n] + /","
", $ Arr ['txt ']);


Could you help me write another statement that converts spaces to & nbsp?

$ S = str_replace ('','', $ s );

The nl2br function is to add'
'String

The front-end uses js to process replace (/\ n | \ r | (\ r \ n) | (\ u0085) | (\ u2028) | (\ u2029)/g ,"
");

$ Arr ['txt '] = nl2br ($ arr ['txt']);

Str_replace (char (13 )),"
", $ Arr [txt]);
Yes, because php does not have the char Function and only has the chr function.
However, it is inappropriate to write chr (13), because the linux text does not have this "carriage return" character, so if you write it yourself, you should write it.
$ Arr ['txt '] = preg_replace ("/[\ r \ n] + /","
", $ Arr ['txt ']);


I found the statement you gave: $ arr ['txt '] = preg_replace ("/[\ r \ n] + /","
", $ Arr ['txt ']);
Yes, you can convert the carriage return
And wrap the line on the final webpage.
But it seems to convert several lines of carriage returns into
The final result is: when I posted the post, there were several blank rows, but only one line was changed during the last display. It is impossible to increase the interval when you want to increase the number of empty rows.

If you want to convert a carriage return to
What should I do?

After experiment, I found that the nl2br function is acceptable, but it won't work if it does:

$ Arr [txt] = nl2br ($ arr [txt]);

But you can do this:

$ Str = nl2br ($ arr [txt]);

Then, $ str is displayed, and a line break is displayed. I don't know why.

In addition, it can also interpret multiple carriage returns as multiple
Multiple line breaks are displayed, which is exactly what I need. Thank you!

Str_replace ("", "", $ arr [txt]);

After experiment, I found that the nl2br function is acceptable, but it won't work if it does:

$ Arr [txt] = nl2br ($ arr [txt]);

But you can do this:

$ Str = nl2br ($ arr [txt]);

Then, $ str is displayed, and a line break is displayed. I don't know why.

In addition, it can also interpret multiple carriage returns as multiple
Multiple line breaks are displayed, which is exactly what I need. Thank you!


$ Arr [txt] is not supported? How do you display it? Then I moved my hands and feet on it.

This is my success

$select = mysql_query("select....") while($arr = mysql_fetch_array($result)) {    echo str_replace(" ", " ", $arr['txt']); }

I found the answer on the Internet, that is, when saving it to the database, it is not processed. Use this statement to read the data:

CurrentContent = conn. myReader ["Content"]. ToString (). Replace (Convert. ToString (char) 13 ),"
");

But this is an object-oriented method that I don't quite understand. What are process-oriented statements translated?


Read the Content field data, convert it to the string format, and replace the carriage return character
.

Direct use

Expand

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.