PHP: Converting a wall of text to arrays (preg_match or something similar?)

I have attached a snippit of what I do when I get status. What I'd LIKE to do is convert this all to an array. It doesn't need to be fancy, but everything that really isn't a space, can be converted to an array, so

------------inbound------------ would be arrayname[0], ------------outbound----------- would be [1], Total [2], 0 being [3], etc... Until we have it ALL.

I'm thinking preg_match might work good here but I'm not 100% sure how to use it in this kind of method. Any ideas?

Thanks!
Traffic    ------------inbound------------  ------------outbound-----------
                 rcpts      msgs      kbytes      rcpts      msgs      kbytes
      Total          0         0         0.0      20289     20289     34257.7
  Last Hour          0         0         0.0      20095     20095     33929.6
   Top/Hour          0         0         0.0      20095     20095     33929.6
  Last Min.          0         0         0.0       2901      2901      4792.5
   Top/Min.          0         0         0.0       8133      8133     14082.1
 Connections    active       top     maximum  Domain       cached     pending
    Inbound          0         0          30    Names      118159        1002
   Outbound       1200      1200        1200
     Queues      rcpts   domains      kbytes  Spool        in use    recycled
       SMTP     160431     42844    194690.1    Files         473           0
      other          0         0         0.0    Init.                complete
     Status    running    Started  2010-09-18 15:12:54    Uptime  32  0:05:28

Open in new window

LVL 7
VallerianiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Terry WoodsIT GuruCommented:
Does this provide what you want?:

$my_array = preg_split("/\s+/", $source_string);
Terry WoodsIT GuruCommented:
Note that "in use", "Last Hour" and "Last Min." will of course be split into 2 array values eg "in" and "use"
Justin MathewsCommented:
If the data is in data.txt:

<?php
$handle = fopen("data.txt", "r");

$arr = array();

while ($str = fgets ($handle)) {
      array_push($arr, preg_split('/\s+/', $str, -1, PREG_SPLIT_NO_EMPTY));
}

print_r($arr);
?>
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

Ray PaseurCommented:
Can you please show us what the array would look like for the text posted above?
VallerianiAuthor Commented:
I had to modify the coding just a tad to

<?php

$result = shell_exec('sudo /usr/sbin/pmta show status');
$arr = array();
array_push($arr, preg_split('/\s+/', $result, -1, PREG_SPLIT_NO_EMPTY));

print_r2($arr);

function print_r2($val){
        echo '<pre>';
        print_r($val);
        echo  '</pre>';
}
?>

print_r2  just makes it easier to read in a browser ;)

It works good, I believe.

$my_array = preg_split("/\s+/", $result); also works but there are some extra ones at the beginning and end that are blank.



Array
(
    [0] => Array
        (
            [0] => MTA
            [1] => v2.0
            [2] => status
            [3] => on
            [4] => customer
            [5] => on
            [6] => 2010-09-19
            [7] => 07:03:43
            [8] => Traffic
            [9] => ------------inbound------------
            [10] => ------------outbound-----------
            [11] => rcpts
            [12] => msgs
            [13] => kbytes
            [14] => rcpts
            [15] => msgs
            [16] => kbytes
            [17] => Total
            [18] => 352841
            [19] => 177
            [20] => 214.3
            [21] => 220936
            [22] => 220936
            [23] => 380974.2
            [24] => Last
            [25] => Hour
            [26] => 0
            [27] => 0
            [28] => 0.0
            [29] => 6716
            [30] => 6716
            [31] => 11662.7
            [32] => Top/Hour
            [33] => 235251
            [34] => 118
            [35] => 142.9
            [36] => 111473
            [37] => 111473
            [38] => 191214.6
            [39] => Last
            [40] => Min.
            [41] => 0
            [42] => 0
            [43] => 0.0
            [44] => 0
            [45] => 0
            [46] => 0.0
            [47] => Top/Min.
            [48] => 117652
            [49] => 59
            [50] => 71.4
            [51] => 11540
            [52] => 11540
            [53] => 19711.4
            [54] => Connections
            [55] => active
            [56] => top
            [57] => maximum
            [58] => Domain
            [59] => cached
            [60] => pending
            [61] => Inbound
            [62] => 0
            [63] => 0
            [64] => 30
            [65] => Names
            [66] => 14956
            [67] => 39967
            [68] => Outbound
            [69] => 13
            [70] => 1200
            [71] => 1200
            [72] => Queues
            [73] => rcpts
            [74] => domains
            [75] => kbytes
            [76] => Spool
            [77] => in
            [78] => use
            [79] => recycled
            [80] => SMTP
            [81] => 131905
            [82] => 39124
            [83] => 159728.7
            [84] => Files
            [85] => 177
            [86] => 0
            [87] => other
            [88] => 0
            [89] => 0
            [90] => 0.0
            [91] => Init.
            [92] => complete
            [93] => Status
            [94] => running
            [95] => Started
            [96] => 2010-08-17
            [97] => 21:50:23
            [98] => Uptime
            [99] => 32
            [100] => 9:13:20
        )

)

Open in new window

Ray PaseurCommented:
This sequence should get rid of the empties.  HTH, ~Ray
<?php // RAY_temp_mickey.php
error_reporting(E_ALL);

// SAMPLE TEST DATA
$txt = <<<EOTXT
Traffic    ------------inbound------------  ------------outbound-----------
                 rcpts      msgs      kbytes      rcpts      msgs      kbytes
      Total          0         0         0.0      20289     20289     34257.7
  Last Hour          0         0         0.0      20095     20095     33929.6
   Top/Hour          0         0         0.0      20095     20095     33929.6
  Last Min.          0         0         0.0       2901      2901      4792.5
   Top/Min.          0         0         0.0       8133      8133     14082.1
 Connections    active       top     maximum  Domain       cached     pending
    Inbound          0         0          30    Names      118159        1002
   Outbound       1200      1200        1200
     Queues      rcpts   domains      kbytes  Spool        in use    recycled
       SMTP     160431     42844    194690.1    Files         473           0
      other          0         0         0.0    Init.                complete
     Status    running    Started  2010-09-18 15:12:54    Uptime  32  0:05:28
EOTXT;

// HANDLE TWO SPECIAL CASES
$txt = str_replace('Last Hour', 'Last-Hour', $txt);
$txt = str_replace('Last Min.', 'Last-Min.', $txt);

// CONVERT ALL WHITESPACE TO A SINGLE BLANK
$txt = preg_replace('/\s\s+/', ' ', $txt);

// MAKE THE ARRAY
$arr = explode(' ', $txt);

// SHOW THE WORK PRODUCT
echo "<pre>";
print_r($arr);

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
VallerianiAuthor Commented:
Thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.