Home > Database > Mysql Tutorial > How Can I Preserve Line Breaks from a TextArea When Outputting Text?

How Can I Preserve Line Breaks from a TextArea When Outputting Text?

Susan Sarandon
Release: 2024-12-30 22:47:19
Original
798 people have browsed it

How Can I Preserve Line Breaks from a TextArea When Outputting Text?

Maintaining Line Breaks from a TextArea

When users input text into a text area, line breaks are often lost when the text is outputted. To preserve these line breaks, two solutions can be implemented:

1. PHP Function: nl2br()

The nl2br() function converts newlines (nr) to HTML line breaks (
).

Example:

echo nl2br("This\r\nis\n\ra\nstring\r");

// Output:
This<br />
is<br />
a<br />
string<br />
Copy after login

2.
 Tags</h3>
<p>Wrapping the input in <pre class="brush:php;toolbar:false">
tags ensures that the text is displayed exactly as it was entered, including line breaks.

Reference: W3C Wiki - HTML/Elements/pre

The above is the detailed content of How Can I Preserve Line Breaks from a TextArea When Outputting Text?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template