将 Php 函数结果与 nl2br 混淆
我只是尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您误解了
passthru($cmd)
的作用。它执行$cmd
,但将stdout
直接发送到浏览器 - 您不能以字符串形式返回结果。相反,它返回被调用的$cmd
的返回代码。如果您想捕获输出,使用
exec
,并通过引用传递$output
数组。You're misunderstanding what
passthru($cmd)
does. It executes$cmd
, but sendsstdout
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.