Home >Backend Development >PHP Tutorial >How Can I Preserve Line Breaks from a TextArea Input?
Preserving Line Breaks from TextArea
When working with text input, it's often crucial to retain the line breaks inserted by the user. However, when retrieving text from a
Method 1: PHP's nl2br() Function
PHP provides the nl2br() function that converts line breaks (rn) into HTML line breaks (
). By passing the text from the
echo nl2br("This\r\nis\n\ra\nstring\r");
Output:
This<br /> is<br /> a<br /> string<br />
Method 2: Using This method ensures that the text is displayed exactly as it was entered into the The above is the detailed content of How Can I Preserve Line Breaks from a TextArea Input?. For more information, please follow other related articles on the PHP Chinese website! Tags</strong></p>
<p>Another approach involves wrapping the text input with HTML <pre class="brush:php;toolbar:false"> tags. This prevents the browser from interpreting any line breaks or formatting characters.</p>
<pre class="brush:php;toolbar:false"><pre class="brush:php;toolbar:false">This
is
a
string