sorry if i misunderstood the question how ever i think this could be the answer you are looking for... :)
Version 1:
#executes the comand and stores in array
$testExec = exec("df -h --total" ,$testArray);
#moves to the end off the array
$testArrayEnd = end($testExec);
#splits the last line into an array
$stringArray = explode(' ',$testArrayEnd);
foreach($stringArray as $string){
#checks if the string contains a %
if (preg_match("/%/", $string)) {
#there is only one % so this is what you're looking for
$percentage = $string;
var_dump($percentage);
}
}
PS. @ReynierPM you don't require a shell_exec to get a full output return... you just have to define a array where you want to store the data in... :) and granted its not a string but you could easily transform it into one by using implode()
Version 2:
Sorry If you disagree but i don't feel comfortable just editing @Nazariy answer as I'm adding/changing to much (at this stage see edit history:).
#thanks go out to ReynierPM
#returns string with every entry being separated by a newline
$output = shell_exec('df -h --total');
$ArrayFull=(array_map(function($line){ /*<-- *¹ & *² */
$elements=preg_split('/\s+/',$line); /*<--- *4 */>
return(array(
'filesystem' => $elements[0],
'1k-blocks' => $elements[1],
'used' => $elements[2],
'available' => $elements[3],
'use%' => $elements[4],
'mounted_on' => $elements[5]
));
},explode("\n",$output))); /*(<--- *³)*/
#removes bloat
unset($ArrayFull[0]);
#Rebase array keys https://stackoverflow.com/questions/5943149/rebase-array-keys-after-unsetting-elements
$ArrayFull=array_values($ArrayFull);
#if you only want the last value ;)
$lastVallue = end($ArrayFull);
Explanation:
*¹
array_map applies it too all values in the array
"array_map — Applies the callback to the elements of the given arrays"
*²
We first give it a callback function which will be called for every element, and pass it the variable $line (we use this to store the line created by the explode)
*³
We use the array_maps to explode by \n (to create a array entry for evry new line) (We offcourse do an explode on $data)
*4
So now that every line is separated... We split the separated lines into substrings and store them in new variables.
preg_split('/\s+/',$line) splitting $line into an array without having to deal with the issue of multiple whitspaces. s standing for space.
Sorry for the formatting... will edit latter :)