Home > Database > Mysql Tutorial > How Can I Preserve Line Breaks in Text Area Output?

How Can I Preserve Line Breaks in Text Area Output?

Mary-Kate Olsen
Release: 2025-01-01 02:09:09
Original
454 people have browsed it

How Can I Preserve Line Breaks in Text Area Output?

Preserving Line Breaks in Text Area Output

When working with text areas, it is often desirable to preserve line breaks when the entered text is displayed. However, by default, line breaks are lost during output. Here are two effective solutions to overcome this issue:

1. Utilizing the nl2br() Function:

The nl2br() function converts new line characters ("n") into HTML line break tags (
). By processing the text through this function, you can ensure that line breaks are maintained when displayed.

For example:

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

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

2. Encapsulating Text in

 Tags:

Wrapping the input text within

 tags preserves its formatting, including line breaks. This approach is particularly useful when you want to display raw text or maintain its original structure.

Refer to: W3C Wiki - HTML/Elements/pre

The above is the detailed content of How Can I Preserve Line Breaks in Text Area Output?. 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