dima1236 Posted February 28, 2009 Share Posted February 28, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/147352-help-on-extracting-specific-data-from-text/ Share on other sites More sharing options...
.josh Posted February 28, 2009 Share Posted February 28, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/147352-help-on-extracting-specific-data-from-text/#findComment-773448 Share on other sites More sharing options...
dima1236 Posted February 28, 2009 Author Share Posted February 28, 2009 thanks alot, but the issue that it actually not in FILE it is in $text, is there something like FILE function to extract it ? thanks Quote Link to comment https://forums.phpfreaks.com/topic/147352-help-on-extracting-specific-data-from-text/#findComment-773458 Share on other sites More sharing options...
.josh Posted February 28, 2009 Share Posted February 28, 2009 maybe you can first explode $text at \n to get your initial array that file() would have given you. Quote Link to comment https://forums.phpfreaks.com/topic/147352-help-on-extracting-specific-data-from-text/#findComment-773467 Share on other sites More sharing options...
dima1236 Posted February 28, 2009 Author Share Posted February 28, 2009 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/147352-help-on-extracting-specific-data-from-text/#findComment-773471 Share on other sites More sharing options...
.josh Posted February 28, 2009 Share Posted February 28, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/147352-help-on-extracting-specific-data-from-text/#findComment-773501 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.