Why does PHP text echo'd lose formatting?

Any ideas why formatted text from the database when the php echoout loses its formatting, i.e. no new lines? Thanks!

enter image description here

+6
source share
6 answers

Use nl2br() .

Newlines are ignored by the browser. This is why you see all text without line breaks. nl2br () converts newlines to <br /> tags, which are displayed as newlines in browsers.

If you want to display text in <textarea> , you do not need to convert all newlines to <br /> . In any case, if you do this ... you will see " <br /> " as text in new lines.

+26
source

Since there are no html tags for formatting! Try nl2br .

+14
source

You can try adding the nl2br() function ...

something like this: echo nl2br($your_text_variable);

It should work; -)

+9
source

Cause

This is the default behavior for all user agents. If you look at the source of the page, you will see that your text has the same formatting as in the database (or in the text box).

The reason for your confusion is probably because you once saw the text in the <textarea> , which displays pre-formatted text, does not interpret the tags, and in the other case the text is interpreted (spaces are not important in this case).

No new lines are displayed in browsers unless specifically specified — using the <br> tag or block level tags.

No tags == No new lines.

Correction

If you save preformatted text in a database,
you must wrap the output in a <pre> .

You can convert formatting characters to desired HTML tags using a set of functions such as nl2br , str_replace , etc.

You can also fix your structure for storing HTML in a database instead of plain text (however, markup looks like the best solution).

See a similar question:

+6
source

The difference between the two images you display is that one has text in <textarea></textarea> and the other doesn't ... if you want 1: 1: <textarea><?php echo $yourVariable;?></textarea>

+3
source

It outputs what you say to output. If the text is pre-formatted, put it in the HTML <pre></pre> in your script output.

+2
source

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


All Articles