【发布时间】: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>
<Information>
Traceroute output:
<br>
<?php echo $_POST["Traceroute"]; ?> </Information>
<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:
<Information>
<?php echo nl2br($_POST["Traceroute"]); ?> </Information>
这是现在的输出:
</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 默认没有任何意义...