无法删除php中的换行符shell_exec()returnvalue

时间:2015-12-04 09:18:37

标签: php linux shell exec line-breaks

我试图从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字符,但没有运气。

1 个答案:

答案 0 :(得分:0)

首先确保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不是eth的行,然后尝试使用它来替换换行符$ethTest = preg_replace("/\r\n|\r|\n/",'',$ethTest)