【问题标题】:Passing TextArea to php comes out as one line string将 TextArea 传递给 php 以一行字符串形式出现
【发布时间】:2016-04-04 10:45:30
【问题描述】:

我对如何让它工作有点茫然,因为我不是真正的 PHP 人。

基本上,在我的表单中,我的 HTML 中有一个 TextArea,用户将从他们的命令行粘贴到 TraceRoute 中。然后将它传递给我的 PHP 表单(它变成了 xml ......这并不重要)。

但是,tracert 以单行字符串的形式出现,而不是单独的行。这使得阅读变得非常困难。

所以我需要一种方法让 traceroute 像在 TextArea 框中一样准确地显示出来。

这是我的 html 代码 (submit.html)

<html>
<body>
<form action="convert2xml.php" method="post">
Traceroute:
<textarea rows="5" cols="50" name="Traceroute"></textarea>
<br>
<input type="Submit">
</form>
</body>
</html>

这是我处理数据的 PHP 文件 (convert2xml.php)。

<html>
<body>
&#60;Information&#62;
Traceroute output:
<br>
<?php echo $_POST["Traceroute"]; ?> &#60;/Information&#62;
<br>

正如您所见, 已被替换为 html 代码,这就是将其变成漂亮的 XML 布局的原因(在本例中,它位于名为 Information 的 xml 标记中)。

一个示例输入是(我已经编辑了一些 IP 和域):

    C:\Users\******>tracert 8.8.8.8

    Tracing route to google-public-dns-a.google.com [8.8.8.8]
    over a maximum of 30 hops:

      1     1 ms     3 ms     1 ms  192.168.0.1
      2    12 ms    12 ms     8 ms  **.**.**.**
      3     9 ms    12 ms     9 ms  **.**.**.**
      4    13 ms    13 ms    13 ms  example-doman.name [**.**.**.**]
      5    15 ms    15 ms    14 ms  example-doman.name [**.**.**.**]
      6    12 ms    14 ms    13 ms  **.**.**.**
      7    14 ms    13 ms    16 ms  **.**.**.**
      8    11 ms    19 ms    15 ms  google-public-dns-a.google.com [8.8.8.8]

    Trace complete.

但我得到的是一个连续的字符串:

<Information>C:\Users\******>tracert 8.8.8.8 Tracing route to google-public-dns-a.google.com [8.8.8.8] over a maximum of 30 hops: 1 1 ms 3 ms 1 ms 192.168.0.1 2 12 ms 12 ms 8 ms **.**.**.** 3 9 ms 12 ms 9 ms **.**.**.** 4 13 ms 13 ms 13 ms example-doman.name [**.**.**.**] 5 15 ms 15 ms 14 ms example-doman.name [**.**.**.**] 6 12 ms 14 ms 13 ms **.**.**.** 7 14 ms 13 ms 16 ms **.**.**.** 8 11 ms 19 ms 15 ms google-public-dns-a.google.com [8.8.8.8] Trace complete. </Information>

我查看了 nl2br,但这对我没有帮助,因为我必须在 traceroute 行的末尾手动输入“\n”才能使其工作。

我唯一能想到的就是一个循环,它检查字符串中的 ascii 换行代码,然后添加一个“\n”或
。还是会在文本区域的每一行周围添加“”,然后让html在每个“”处添加

但是必须有一些更简单的方法吗?有什么想法吗?

************更新*********

@FastTurtle 提供的正确答案

似乎我把它复杂化了。

n2lbr 非常适合我的目的。

这是更新后的 PHP:

    &#60;Information&#62;
    <?php echo nl2br($_POST["Traceroute"]); ?> &#60;/Information&#62;

这是现在的输出:

</Information>C:\Users\******>tracert 8.8.8.8

Tracing route to google-public-dns-a.google.com [8.8.8.8]
over a maximum of 30 hops:

1 1 ms 3 ms 1 ms 192.168.0.1
2 12 ms 12 ms 8 ms **.**.**.**
3 9 ms 12 ms 9 ms **.**.**.**
4 13 ms 13 ms 13 ms example-doman.name [**.**.**.**]
5 15 ms 15 ms 14 ms example-doman.name [**.**.**.**]
6 12 ms 14 ms 13 ms **.**.**.**
7 14 ms 13 ms 16 ms **.**.**.**
8 11 ms 19 ms 15 ms google-public-dns-a.google.com [8.8.8.8]

Trace complete. </Information>

【问题讨论】:

  • 提示:空白 in HTML 默认没有任何意义...

标签: php html text ascii


【解决方案1】:

使用

echo nl2br($_POST["Traceroute"]);

更多关于 nl2br 函数http://php.net/manual/en/function.nl2br.php

希望对你有帮助:)

【讨论】:

  • 嗯....现在我觉得很傻!因为 nl2br 可以很好地满足我的目的。不知道为什么我以前不能让它工作。认为我已经看得太久了,而且在我的脑海中过于复杂了:) 非常感谢@FastTurtle。我会将其标记为已回答。
猜你喜欢
  • 2013-02-15
  • 1970-01-01
  • 2013-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-27
  • 2014-02-02
  • 1970-01-01
相关资源
最近更新 更多