将TextArea传递给php将显示为一行字符串


Passing TextArea to php comes out as one line string

我有点不知所措,因为我不是一个真正的PHP人员。

基本上在我的表单中,我的HTML中有一个TextArea,用户将从命令行粘贴到其中的TraceRoute。然后这个get被传递到我的PHP表单(在那里它被转换成xml……这并不重要)。

然而,示踪剂是作为一个单行字符串出现的,而不是单独的行。这使它很难阅读。

因此,我需要一种方法来精确地显示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"或<br>。或者在文本区域的每一行周围添加一行",然后让html添加<br>在每个"

但一定有更简单的方法可以做到这一点吗?有什么想法吗?

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

正确答案由@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>

使用

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

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

希望有帮助:)