line breaks in a textarea

前端 未结 13 1658
北荒
北荒 2020-11-27 17:58

I know when saving a textarea you can use the nl2br() or str_replace to change the /n to br tags etc. However what im not sure about how to insert line breaks into a textare

相关标签:
13条回答
  • 2020-11-27 18:26

    From PHP using single quotes for the line break worked for me to support the line breaks when I pass that var to an HTML text area value attribute

    PHP

    foreach ($videoUrls as $key => $value) {
      $textAreaValue .= $value->video_url . '\n';
    }
    

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    HTML/JS

    $( document ).ready(function() {
        var text = "<?= htmlspecialchars($textAreaValue); ?>";
        document.getElementById("video_urls_textarea").value = text;
    });
    
    0 讨论(0)
  • 2020-11-27 18:27
    <?php
    $smarty = new Smarty;
    $smarty->assign('test', "This is a \n Test");
    $smarty->display('index.tpl');
    ?>
    

    In index.tpl

    {$test|nl2br}
    

    In HTML

    This is a<br />
    test
    
    0 讨论(0)
  • Some wrong answers are posted here. instead of replacing \n to <br />, they are replacing <br /> to \n

    So here is a good answer to store <br /> in your mysql when you entered in textarea:

    str_replace("\n", '<br />',  $textarea);
    
    0 讨论(0)
  • 2020-11-27 18:30

    What I found works in the form is str_replace('&lt;br&gt;', PHP_EOL, $textarea);

    0 讨论(0)
  • 2020-11-27 18:31

    The simple way:

    1. Use this to insert into mysql:

      $msg = $_GET['msgtextarea']; //or POST and my msg field format: text
      $msg = htmlspecialchars($msg, ENT_QUOTES);

    2. And use this for output:

      echo nl2br($br['msg']);

    0 讨论(0)
  • 2020-11-27 18:32

    You can use following code:

    $course_description = nl2br($_POST["course_description"]);
    $course_description = trim($course_description);
    
    0 讨论(0)
提交回复
热议问题