简体   繁体   中英

execute linux command and store the output in php array

I am making a php website used for monitor a Linux server, so i used linux commands for memory usage like (free) & (cat /proc/meminfo),for Disk storage, network, users etc... i can execute Linux command with php commands like exec and shell_exec and back-tick, etc... but how to store the output in php array ? and how to move each element from the array to string or int variable to perform some string or int functions on it ?

i try this ,

    // 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>";

but i want to get the result in int variable , and if there is a better way for that?

Your life will be easy with Regular Expressions .

<?php

// Why cat in a shell to get info from one file? use the right/easy function!
$data = file_get_contents('/proc/meminfo');

// PCRE will save you in many cases!
preg_match_all("/^([^:]+):\s+([0-9]+)\s*([a-z]+)?$/Umi",$data,$meminfo,PREG_SET_ORDER);

// All you need is here
var_dump($meminfo);

Your question about int and string is a common mistake from who not read the section about types in PHP page . I indicate strongly that you read the basic section manual as many of your questions about data manipulation will be answered. PHP will evaluate data types in accordance to the function used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM