Can I just insert formated text into database and retrieve it as that? Should formating ( like new line ) appear on website after pulled from database? I have tried it and it seems that is not case.
How to accomplish that?
Thanks
Can I just insert formated text into database and retrieve it as that? Should formating ( like new line ) appear on website after pulled from database? I have tried it and it seems that is not case.
How to accomplish that?
Thanks
If you want it to be formatted as is inserted in the database you could output it to a <pre>
tag to preserve formatting.
If you just want to have the newline replaced by a html tag, you can do that easily:
<?php
//be on the safe side, as suggested by Christian Sciberras
$sanitized = htmlspecialchars($databaseContent, ENT_QUOTES);
echo str_replace(array("
", "
"), array("<br />", "<br />"), $sanitized);
Why?
I use str_replace()
in this case to show you how it's done. I usually don't replace the new line with a break but with a </p><p>
(and this is why I didn't go for a nl2br()
solution)
If you think you'd want paragraphs instead of line breaks, just change the replacement.