dongwu9063 2013-10-11 08:09
浏览 81
已采纳

额外的空格和换行符PHP自动删除TEXTAREA

My code for the input php file is the following.

<!DOCTYPE html>
<html>
  <body>
    <form name="input" action="welcome.php" method="post">
      Comment: <textarea name="input" rows="5" cols="40"></textarea>
      <input type="submit" value="Submit">
    </form> 
  </body>
</html>

For the output code it is the following.

<html>
  <body>
    Welcome <?php $a=$_POST["input"]; echo $a; ?><br>
  </body>
</html>

When anything with extra spaces and newline are inputted, it automatically gets removed. For example :

When I input:

abcd
cda xyzb

Output is:

Welcome abcd cda xyzb

  • 写回答

4条回答 默认 最新

  • dsa89029 2013-10-11 08:17
    关注

    This is because new line characters are represented as , in the sourcecode you'll see new lines. Whitespaces get truncated if one follows another in HTML.

    I suggest you to use the <pre> tag, which does not only save the new lines (like php's nl2br()) but also preserves the whitespaces.

    Using <pre>:

    <html>
    <body>
    <pre class="yourStyleForThisPreFormattedText">
    Welcome <?php $a = $_POST["input"]; 
    echo $a; ?>
    </pre>
    </body>
    </html>
    

    Using special chars (&nbsp;) and PHP functions:

    <html>
    <body>
    Welcome <?php $a = nl2br(str_replace(' ', '&nbsp;', $_POST["input"]), true); 
    echo $a; ?>
    </body>
    </html>
    

    Notice:

    For HTML4 and HTML5 use nl2br($str, true);, for XHTML4 use nl2br($str); - the difference is in the output: <br> and <br />. See http://php.net/nl2br

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog