Jump to content

Help on extracting specific data from Text


dima1236

Recommended Posts

Hi guys, i am php-newbie, i got a questions if i have : for example this in file or this is what i get return from a function how can i extract only the PID's to like Array or something?

i need to get only this numbers : 4739 4750 4755 4766 .

thanks in advance for help

 

Warning: bad ps syntax, perhaps a bogus '-'? See http://procps.sf.net/faq.html

USER      PID %CPU %MEM    VSZ  RSS TTY      STAT START  TIME COMMAND

test      4739  0.0  0.1  4692  1876 pts/0    S    23:43  0:00 -sh

test      4750  0.0  0.4  10236  5156 pts/0    S    23:43  0:00 Xvnc4 :2 -deskt

test      4755  0.0  0.1  4380  1560 pts/0    S    23:43  0:00 vncconfig -icon

test      4766  0.0  0.1  3716  1040 pts/0    R+  23:45  0:00 ps -ux

 

use file to put each line of the file into an array element.  Use a foreach loop on that array.  In the foreach loop, explode at the space.  You might possibly have to use preg_split instead, using \s.  Your number will be the 2nd element of the explode/preg_split.

i get an eror

Warning: file() expects parameter 1 to be string, array given in /var/www/test1.php on line 11

 

<?php
   include('SSH2.php');

   $ssh = new Net_SSH2('192.168.58.133');
   if (!$ssh->login('test', 'test')) {
       exit('Login Failed');
   }

   $pizza = ($ssh->exec('ps -ux'));
   $pieces = explode(" ", $pizza);
   $lines = file($pieces);

?>

if your data is already in a string you don't need file at all.  If $pizza is what you are saying has that data, then try

 

$lines = explode("\n", $pizza);

 

to get each line into an array.  Then for each line, explode at the space.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.