Display mysql newline in HTML

The specific fields in our mysql db seem to contain newlines, so if I SELECT, for a single SQL call, something like the following will be returned:

Life to be sure is nothing much to lose

But young men think it is and we were young

If I want to keep line breaks when displaying this field on a web page, is it a standard solution to write a script to replace '\ n \ r' with br HTML tag or is there a better way?

Thank!

+2
source share
3 answers

Assuming PHP is here ...

nl2br()adds in <br />for everyone \n. Remember to avoid content first to prevent XSS attacks. See below:

<?php echo nl2br(htmlspecialchars($content)); ?>
+9
source

HTML - . , , (, <pre> white-space:pre). HTML <br> . , xA (newline, linefeed, LF, \n) / xD ( , CR, \r) HTML <br>.

"\n" "<br>".

+3

<pre>.. </pre>.

+2

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


All Articles