2011-08-01 65 views
2

混淆PHP函數的結果,我只是用PHP試驗,以我準備了一些即將開展的項目和我遇到這將不會<br />插入它,即使它是一個多行字符串的字符串。與nl2br

的代碼是簡單的PHP(我已經包含在簡單的HTML標籤)

$ping = passthru('ping www.google.com'); 
$ping = htmlspecialchars_decode($ping); 
$ping = strip_tags($ping); 
$ping = nl2br($ping); 
echo $ping; 

結果是一個多行字符串,但沒有任何<br />標籤,但添加的頁面的源代碼顯示結果作爲一個多行字符串,所以肯定有多行,但nl2br()沒有做任何事情。

頁源(當我貼在這裏已經神祕地添加額外的空格線)

<html> 

    <head> 

     <title>Derp</title> 



    </head> 

    <body><p> 



Pinging www.l.google.com [209.85.227.147] with 32 bytes of data: 

Reply from 209.85.227.147: bytes=32 time=44ms TTL=48 

Reply from 209.85.227.147: bytes=32 time=28ms TTL=48 

Reply from 209.85.227.147: bytes=32 time=40ms TTL=48 

Reply from 209.85.227.147: bytes=32 time=29ms TTL=48 



Ping statistics for 209.85.227.147: 

    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), 

Approximate round trip times in milli-seconds: 

    Minimum = 28ms, Maximum = 44ms, Average = 35ms 

</p> 

    </body> 

</html> 

以及網頁上顯示的實際字符串:

Pinging www.l.google.com [209.85.227.147] with 32 bytes of data: Reply from 209.85.227.147: bytes=32 time=30ms TTL=48 Reply from 209.85.227.147: bytes=32 time=29ms TTL=48 Reply from 209.85.227.147: bytes=32 time=28ms TTL=48 Reply from 209.85.227.147: bytes=32 time=31ms TTL=48 Ping statistics for 209.85.227.147: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 28ms, Maximum = 31ms, Average = 29ms 

廣泛的谷歌搜索我所能後找到的人誰不使用nl2br()時,他們應該是

缺少什麼我在這裏?

+0

所以我實際上沒有做任何與$平?它沒有分配任何東西? 編輯:果然,你是對的。沒有真正意識到它是這樣做的,謝謝:) – Sam

+1

@Sam:嘗試'exec'或反引號。 – webbiedave

+0

那就是我第一次嘗試:) exec只顯示輸出的最後一行,所以我切換到passthru,但現在我意識到我正在做的全部錯誤。 – Sam

回答

3
<?php 
$ping = `ping www.google.com`; 
$ping = nl2br($ping); 
echo $ping; 
?> 

<br /> 
Pinging www.l.google.com [209.85.147.104] with 32 bytes of data:<br /> 
<br /> 
Reply from 209.85.147.104: bytes=32 time=24ms TTL=53<br /> 
Reply from 209.85.147.104: bytes=32 time=23ms TTL=53<br /> 
Reply from 209.85.147.104: bytes=32 time=23ms TTL=53<br /> 
Reply from 209.85.147.104: bytes=32 time=25ms TTL=53<br /> 
<br /> 
Ping statistics for 209.85.147.104:<br /> 
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),<br /> 

Approximate round trip times in milli-seconds:<br /> 
    Minimum = 23ms, Maximum = 25ms, Average = 23ms<br /> 
1

您誤解了passthru($cmd)的功能。它執行$cmd,而是直接發送stdout到瀏覽器 - 你得到結果返回爲字符串。而是返回被調用的返回碼$cmd

如果要捕獲輸出,請參考use exec,並傳遞$output數組。