I have PHP set up for a textarea that prints text from a directory to separate divs.
<?php
$content = implode("<br /><hr class='separator'>",array_map(function ($v) {
return file_get_contents($v);
}, glob(__DIR__ . "/posts/*.txt")));
echo '<div>';
// echo htmlentities($content);
echo '</div>';
?>
When I use htmlentities (commented out above), it disables my line break (and also my hr separator code).
I tried using \n but it also doesn't work.
How do I keep my <br /><hr class='separator'>
code but still use htmlentities?
My Answer:
You can use the nl2br() function in PHP to convert newlines (\n) to HTML line breaks (
) while still using htmlentities() to escape any HTML characters. Here's how you can modify your code:
php
$content = implode("
", array_map(function ($v) {
return htmlentities(file_get_contents($v));
}, glob(__DIR__ . "/posts/*.txt")));
echo '';
echo nl2br($content);
echo '';
This way, the nl2br() function will convert the newlines in the content to HTML line breaks, while htmlentities() will escape any HTML characters to prevent code execution in the textarea.
Rate this post
4 of 5 based on 6586 votesComments