Home > Software engineering >  Displaying html stored in a database with the appropriate line breaks
Displaying html stored in a database with the appropriate line breaks

Time:09-06

I have code written in a database in several languages, html, css, php, etc. I want it to show on my page with the appropriate line breaks. Like this:

https://cdn.discordapp.com/attachments/668127780927701032/1016459728194375780/unknown.png

So, when I display html, it executes it instead of displaying it, which is normal. So I use htmlspecialchar, except that I can't make a line break anymore, everything is displayed on one line. And I can't br out of this.

How do I do that?

CodePudding user response:

Apparently your string contains literal \n characters, not newlines. So you need to replace that with <br>.

echo str_replace('\n', '<br>', htmlspecialchars($stmt['html']));
  • Related