Search code examples
phphtmlhtml-entitiestxt

How do I line break while also disabling code being executed in textarea?


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?


Solution

  • Call htmlentities() before inserting the HTML separators.

    $content = implode("<br /><hr class='separator'>",array_map(function ($v) {
        return htmlentities(file_get_contents($v));
    }, glob(__DIR__ . "/posts/*.txt")));
    echo "<div>$content</div>";