我有一個腳本,我正在運行一組服務器以使用openwsman從他們那裏取下硬件數據。這是爲了追蹤一個非常開放的環境中的變化,並且最終更新一個mysql數據庫來保存庫存記錄。問題在於,preg_replace在數組中的每個值的開頭和結尾添加了'1'。我相信這些步驟非常不尋常,所以我會解釋每個步驟的作用並在最後提供輸出。preg_replace在數組中的每個子串之前和之後添加'1'
$memsize = shell_exec("wsman enumerate http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/root/dcim/DCIM_MemoryView -h $ipaddress -V -v -c dummy.cert -P 443 -u $user -p $pass -j utf-8 -y basic | grep 'Size'");
$memsize = trim($memsize); #Removes excess spaces at beginning and end of the output.
$memsize = str_replace(' ',',',$memsize); #Replaces spaces between values with a comma.
$memsize = explode(',',$memsize); #Converts the values pulled into an array with comma as the delimiter
$memsizepreg = preg_replace("/[^0-9]/","",$memsize); #Deletes all non-number characters.
$memsizesum = array_sum($memsizepreg); #Gets a sum of all the detected DIMMs.
echo $memsize[0];
echo "<br>";
echo $memsize[1];
echo "<br>";
echo $memsize[2];
echo "<br>";
echo $memsize[3];
echo "<br>";
echo var_dump($memsize);
echo "<br>";
echo $memsizepreg[0];
echo "<br>";
echo $memsizepreg[1];
echo "<br>";
echo $memsizepreg[2];
echo "<br>";
echo $memsizepreg[3];
echo "<br>";
echo var_dump($memsizepreg);
echo "<br>";
echo $memsizesum;
此外,如果我刪除了preg_replace,該array_sum得到一個空白。我輸出變量memsize,memsizepreg和memsize sum變量只是爲了確保拉出的數據是準確的,無論preg_replace在做什麼,都會將1添加到變量的開始和結尾。
輸出:
2048
2048
2048
2048
array(4) { [0]=> string(24) "2048 " [1]=> string(24) "2048 " [2]=> string(24) "2048 " [3]=> string(23) "2048" }
120481
120481
120481
120481
array(4) { [0]=> string(6) "120481" [1]=> string(6) "120481" [2]=> string(6) "120481" [3]=> string(6) "120481" }
481924
有人發佈了此消息,然後消息丟失(仍在我的通知中)。 嘗試使用echo'
';'代替。 我將此添加到我的筆記中,發現變量比實際顯示的長。顯然,它是從第一次運行wsman命令時存在的XML標記中提取內容的。爲什麼它沒有在我的回聲中顯示這個,可能是因爲它實際上並沒有做任何事情。感謝誰提出這個建議。不知道是誰。 – akitosh