[英]Problems with output from exec in PHP
您好,我正在嘗試在PHP中輸出內存使用情況。
我的代碼看起來像這樣:
exec('free -m', $out);
var_dump($out[1]);
list($mem, $total, $used, $free, $shared, $buffers, $cached) = explode(" ", $out[1]);
echo "Memory: " .$used. "/" . $total;
現在的問題是文本打印
Memory: /
var_bump給了我這個:
string(73) "Mem: 3024 1968 1055 0 159 608"
此字符串不應為(73),而應為(29)。 如果我創建自己的數組,則完全沒有問題:
$out = array('','Mem: 3024 2020 1003 0 121 708','');
string(29) "Mem: 3024 1968 1055 0 159 608"
誰能給我解決方案或調試此產品的下一步?
最好的問候,艾倫
當我運行free -m
,我實際上得到了大約73個字符(其中有很多空格):
Mem: 2047 0 2047 0 0 0
我想你會發現這是造成你的空used
和total
: explode
在Mem:
和2047
之間的那些空間的某處拾取空字符串。
一種解決方案是將preg_split
與"/\\s+/"
分隔符一起使用。
刪除空格,例如:
explode(" ", preg_replace('/\s+/', ' ', $out[1]));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.