Keep line break from Textarea

I receive information from a user in a form via PHP on a WordPress site (address specifically), and I want to be able to display the information I collect with the same line breaks (separation of the various components of the address) on another page. It displays correctly if I put it in a text box, but if I get the data and echo in its div, it does not support line breaks. What can I do?

+4
source share
5 answers

you want to use the white space css attribute http://www.w3schools.com/cssref/pr_text_white-space.asp

So you will have something like

<p class="whitespace"><?= $input_from_textarea ?></p> 

using css:

 .whitespace { white-space: pre-wrap; } 

Do not manually clear your entry by replacing new line elements with break tags. This makes your data less reusable and works a lot more.

You can use the php function nl2br() , but it does not give you as much control as display control via css.

+20
source

You can use echo nl2br( $string) to convert \n to <br> HTML line breaks.

+5
source

HTML collapses any number of spaces (except for non-expanding space), including newlines in one space. To support newlines, you will need to replace newlines with <br> or use <pre> tags or use one of the CSS rules with white space to control this behavior.

+2
source

Use the <pre> , for example. But it will look pretty ugly.

Or replace line breaks with <br/>

0
source

use the nl2br () method to skip yours. nl2br ($ text); for more requests check this out http://www.php.net/manual/en/function.nl2br.php

0
source

Source: https://habr.com/ru/post/1484336/


All Articles