Many of the beginners face problem with textarea new line. When you save some data in textarea, you may write data beautifully in many lines (by giving {ENTER} key, new line), then when you save the data inside any database, and when you retrive and display that data, you will notice that, all new line startings are lost.
This is because when you press ENTER and give a new line character '\n' character is used inside textarea. But when you print it back, HTML cannot recognize '\n' character. PHP comes with a handy solution for this. There is a function called nl2br(),which replaces all \n characters with <br/> hence all new lines appear in HTML again!
Its not the only solution, you can also use explode function or something similar to find \n and replace it with a <br/> to comply with HTML
Eg:
<?php
echo nl2br("Sachin\nis great");
?>
Output:
Sachin<br/>is great
Now
<?php
$textarea_data=DATA_FROM_DATABASE;
echo nl2br($textarea_data);
?>
will work for textarea data!
This is because when you press ENTER and give a new line character '\n' character is used inside textarea. But when you print it back, HTML cannot recognize '\n' character. PHP comes with a handy solution for this. There is a function called nl2br(),which replaces all \n characters with <br/> hence all new lines appear in HTML again!
Its not the only solution, you can also use explode function or something similar to find \n and replace it with a <br/> to comply with HTML
Eg:
<?php
echo nl2br("Sachin\nis great");
?>
Output:
Sachin<br/>is great
Now
<?php
$textarea_data=DATA_FROM_DATABASE;
echo nl2br($textarea_data);
?>
will work for textarea data!