Hello I am trying to output my memory use in PHP.
My code looks like this:
exec('free -m', $out);
var_dump($out[1]);
list($mem, $total, $used, $free, $shared, $buffers, $cached) = explode(" ", $out[1]);
echo "Memory: " .$used. "/" . $total;
Now the problem is that the text prints
Memory: /
And the var_bump gives me this:
string(73) "Mem: 3024 1968 1055 0 159 608"
This string should not be (73) but (29).
If I make my own array there is no problems at all:
$out = array('','Mem: 3024 2020 1003 0 121 708','');
string(29) "Mem: 3024 1968 1055 0 159 608"
Can anyone give me a solution or a next step in debugging this?
Best Regards,
Allan
When I run
free -m, I actually get about 73 characters (lots of spaces in there):I think you’ll find that’s what’s causing your empty
usedandtotalvalues:explodeis picking up the empty strings somewhere in those spaces betweenMem:and2047.One solution is to use
preg_splitwith a separator of"/\s+/".