【问题标题】:Can't remove line breaks in php shell_exec() returnvalue无法删除 php shell_exec() 返回值中的换行符
【发布时间】:2015-12-04 09:18:37
【问题描述】:

我正在尝试从 ifconfig 中获取一些数据,但我只将第一行存储在我的变量中。

我跑

//First off, find out whether we're using eth or wlan
$wlanTest = shell_exec('/sbin/ifconfig wlan|egrep -o "wlan|inet addr:[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}|RUNNING"');
$log = "| contents of wlan test: " . $wlanTest;
echo $log;
myLog($log);

我只得到答案的第一行。完整的答案是:

eth
inet addr:192.168.99.193
RUNNING

但我在变量中得到的只是

eth

我尝试使用 php implode() 和 str_replace() 从 $wlanTest 中删除 \n 字符,但没有成功。

【问题讨论】:

  • 您是否尝试将 / 替换为 //?
  • 不,这是 linux cmd 提示符中的特殊换行符吗?我只知道 \n 用于 linux 换行符。

标签: php linux shell exec line-breaks


【解决方案1】:

首先确保 shell_exec() 使用 var_dump 返回你需要的所有数据,因为我看到你的命令

egrep -o "wlan|inet addr:[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}|RUNNING"

选择 wlan not eth 的行,然后尝试用它来替换换行符$ethTest = preg_replace("/\r\n|\r|\n/",'',$ethTest)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-10
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 2014-08-06
    • 2019-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多