2016-04-04 38 views
0

我对如何让这个工作起作用感到有点不知所措,因为不是一个真正的PHP人员。将TextArea传递给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> 

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

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

正如你可以看到<和>已被替换的HTML代码,这就是把它变成一个不错的XML布局(在这种情况下,它是所谓的信息不完整的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> 
+0

提示:空白***在HTML ***并不意味着任何默认情况下... – deceze

回答

2

使用

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

更多nl2br功能http://php.net/manual/en/function.nl2br.php

希望它有助于:)

+0

你仍然需要做更多的事情来获得* indentation *权利,虽然.. – deceze

+0

好吧....现在我觉得很无聊!因为nl2br为我的目的工作得很好。不知道为什么我以前没有工作。想想我只是看着它太长时间了,在我头脑中复杂化:) 非常感谢@FastTurtle。我会将其标记为已回答。 –