将 Php 函数结果与 nl2br 混淆

发布于 2024-11-27 12:14:23 字数 1678 浏览 1 评论 0原文

我只是尝试使用 PHP 来为即将到来的项目做好准备,并且我遇到了一个不会插入
的字符串,即使它是多行细绳。

代码是简单的 PHP(我将其包含在简单的 html 标签中)

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

,结果是一个多行字符串,但没有添加任何
标签,但是,页面源代码显示了结果作为多行字符串,因此肯定有多行,但 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() 的人:他们应该是

我在这里缺少什么?

I'm simply experimenting with PHP to prepare me for some upcoming projects and I've encountered a string which won't have <br /> inserted into it even though it is a multi-line string.

The code is simple PHP (which I've enclosed in simple html tags)

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

The result is a multi-line string but without any <br /> tags added, however, the page source shows the result as a mutli-line string so there's definitely multiple lines there but nl2br() is not doing anything.

Page source (which has mysteriously added extra whitespace lines when I pasted it in here)

<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>

And the actual string shown on the webpage:

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

After extensive Googling all I can find is people who are not using nl2br() when they should be

What am I missing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

韬韬不绝 2024-12-04 12:14:23
<?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 />
<?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 />
仅此而已 2024-12-04 12:14:23

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

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

You're misunderstanding what passthru($cmd) does. It executes $cmd, but sends stdout directly to the browser - you do not get the results back as a string. Instead, it returns the return code of the called $cmd.

If you want to capture output, use exec, and pass an $output array by reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文