使用nl2br混淆Php函数结果

时间:2011-08-01 19:56:47

标签: php command-line nl2br

我只是在尝试使用PHP来为即将到来的项目做好准备,而且我遇到了一个字符串,即使它是一个多行字符串,也不会插入<br />

代码很简单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

经过广泛的Google搜索后,我发现所有人都应该在nl2br()时使用

我在这里缺少什么?

2 个答案:

答案 0 :(得分: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 :(得分:1)

你误解了passthru($cmd)的作用。它会执行$cmd,但会将stdout直接发送到浏览器 - 您将结果作为字符串返回。相反,它返回被调用的$cmd的返回码。

如果您想捕获输出use exec,并通过引用传递$output数组。