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 />
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!