2016-04-04 37 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。我會將其標記爲已回答。 –