我正在制作一个用于监视Linux服务器的php网站,因此我使用了linux命令来获取内存使用情况,如(免费)&(cat/proc/meminfo),用于磁盘存储,网络,用户等...我可以执行Linux命令,如exec和shell_exec和back-tick等php命令......但如何将输出存储在php数组中?以及如何将数组中的每个元素移动到字符串或int变量以对其执行一些字符串或int函数?执行linux命令并将输出存储在php数组中
我试试这个,
// get memort details into array !!!
$last_line = exec('cat /proc/meminfo', $retval);
// this will return just the memory total
echo $retval[1];
echo "<br> <br>";
// this will move the memory total into variable
$memory_total=$retval[0];
echo "<br> <br> the memory total by variable is $memory_total <br><br> ";
implode($memory_total);
//this will give me the memory total in KB
echo substr($memory_total,9);
echo "<br> <br>";
,但我想要得到的结果在int变量,如果有应该是一个更好的办法?
我不明白这个问题。 '$ retval'是一个PHP数组。 – Barmar
'implode($ memory_total)'没有任何意义。语法是'implode($ separator,$ array)'。而且你没有在任何地方分配结果。至于在int变量中获得结果,PHP会在必要时自动将字符串转换为int,但如果需要强制执行,则可以使用显式的'(int)'cast。 – Barmar