3

I have a line of php :

$output = shell_exec('ps aux | grep 9902 | awk \'{print $11" "$2}\'');

print $output; will give the output :

rtmpgw 10089
/usr/bin/vlc 10107
sh 10123
grep 10125

I tried using this next line to put the above output into an array.

$oparray = explode(" ", trim($output));

It works, but not as expected. print_r($oparray); will give the following output :

Array
(
    [0] => rtmpgw
    [1] => 10089
/usr/bin/vlc
    [2] => 10107
sh
    [3] => 10123
grep
    [4] => 10125
)

This confuses me as I would expect an Index number for each value but three of the values appear to be without indices.

So I suppose my question here is two parts

  1. What is going on in the output above
  2. Can anyone help me get a useful array that looks like this :

    Array
    ( [0] => rtmpgw [1] => 10089 [2] => /usr/bin/vlc [3] => 10107 [4] => sh [5] => 10123 [6] => grep [7] => 10125 )

Thanks~

BryanK
  • 1,211
  • 4
  • 15
  • 33

4 Answers4

10

The lines in shell_exec output are separated by the end-of-line symbol - and that's different from normal whitespace, that's why explode doesn't account for it.

You can use preg_split instead, with \s character class matching both normal whitespace and EOL.

$oparray = preg_split('/\s+/', trim($output));
raina77ow
  • 103,633
  • 15
  • 192
  • 229
  • Perfect Array. Simple solution and works good. Thanks so much. – BryanK Sep 24 '13 at 08:41
  • 1
    this solution will not work well for output name those contains whitespace! For example a file name like **Hello world** , this solution will create `[0]=>['Hello','world']` – Ibrahim Oct 21 '14 at 21:32
10

Try this:

<?php
$text = 'rtmpgw 10089
/usr/bin/vlc 10107
sh 10123
grep 10125';

$oparray = preg_split("#[\r\n]+#", $text);

print_r($oparray);
?>
Array
(
    [0] => rtmpgw 10089
    [1] => /usr/bin/vlc 10107
    [2] => sh 10123
    [3] => grep 10125
)
Jason OOO
  • 3,567
  • 2
  • 25
  • 31
  • This worked perfectly, except I had to add trim() to the $text variable, like $response_array = preg_split("#[\r\n]+#", trim($output)); Otherwise, it returned an extra index. – DevOpsSauce Nov 13 '19 at 14:31
1
$array = array();
foreach(preg_split("/((\r?\n)|(\r\n?))/", $subject) as $line){
    $oparray = explode(" ", trim($line));
    $array[] = $oparray[0];
    $array[] = $oparray[1];
} 
Kevin
  • 1,000
  • 2
  • 10
  • 31
  • 2
    Just a pointer, you don't need to declare `$array` if you are using `[]=` – andy Sep 24 '13 at 08:32
  • it's true that you don't need Array(), but I often include it to make sure that the variable is empty. – Manu Apr 09 '14 at 10:15
0

There's no need to use preg_split as this can be achieved with explode and should perform better. Output is separated using end of line characters. Simply use explode with PHP_EOL. Considering, you also wish to have items separated by spaces, you'll also need to do a str_replace.

$array = explode(PHP_EOL, str_replace(' ', PHP_EOL, $output));

If you are left with an empty string at the end of your array, use:

$array = explode(PHP_EOL, rtrim(str_replace(' ', PHP_EOL, $output)));
Dan Bray
  • 7,242
  • 3
  • 52
  • 70