Question

I have a form that has a textarea in it. How would I make new lines in the textarea appear as new paragraphs when I echo out the submitted textarea value?

Was it helpful?

Solution

<?php 

        $textarea = $_POST['textarea'];

       $newarr = explode("\n",$textarea);

      foreach($newarr as $str) {

      echo "<p>".$str."</p>";

    }
?>

OTHER TIPS

echo '<p>' . preg_replace("~[\r\n]+~", '</p><p>', $textarea) . '</p>';

Use nl2br function:

<?php 

echo nl2br($_POST['textarea']);

?>

It will print <br> all newlines

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top