views:

53

answers:

4

Can someone pls show me how to map this correctly? I am trying to understand how to use php explode() and organizing the values in a way that I can retrieve and print them in some organized matter. For each record I want to put a name=value in a particular bucket. I have (7) max buckets per record. Sometimes I have records that won't fill each bucket.

(for example record (2) is missing attributes (5,6,7) and record (3) is missing attribute (4)).

1-Column=host1.colo.sub;2-Column=Fri Aug 13;3-Column=db9.nfl.colo2.;4-Column=00:00:03;5-Column=01:55:02;6-Column=87.24 MB;7-Column=Success;
1-Column=host1.colo.sub;2-Column=Fri Aug 13;3-Column=pdb2.colo2.;4-Column=04:00:02;
1-Column=host1.colo.sub;2-Column=Fri Aug 13;3-Column=gl3_lvm;5-Column=04:48:06;6-Column=54.64 MB;7-Column=Success;

So far I wrote this to view my output:

<?php
$InputFile = file("test.txt");
foreach ($InputFile as $line){
    $pieces = explode(";", $line);
         //print $pieces[0];
         //print $pieces[1];
         //print $pieces[2];
         print $pieces[3];
         //print $pieces[4];
         //print $pieces[5];
         //print $pieces[6];
//print_r($line);
}
?>

I would like to print out similar values for each attribute instead of this where its mixed. Using 'print $pieces[3];'

4-Column=00:00:034-Column=04:00:025-Column=04:48:06
A: 

If your input data is so irregular you would either need to parse it with a bit more code, not just a simple explode, or prepare it so that it works as expected with explode.

In the second case, you could use strpos to find all ";" characters in the string and check if the number after them is in order. If one (or more) number was skipped you should compensate by inserting another ";". This way explode will create an empty array element and all your resulting arrays should be aligned.

silvo
A: 

While it's not exactly clear what you're trying to accomplish, hopefully this code might help you figure out what's going on.

Note that here, I've added another loop inside the main foreach, to iterate over each name/value pair in the line:

<?php
$InputFile = file("test.txt");
$lineCnt = 0;
foreach ($InputFile as $line){
    $lineCnt++;
    echo "Processing line #" . $lineCnt. "\n";
    $pieces = explode(";", $line);
    foreach($pieces as $pair){
    $pair = explode('=',$pair);
        if (!empty($pair[1])){
            print "\t".$pair[0] .' = ' . $pair[1] . "\n";
        }
    }

}

The script will produce nice-looking output if run from the command-line. If you're running it in the browser, you might want to change the \n's to
, and the \t's to   or something.

timdev
A: 

See if this helps...

$splits = explode(';',$_POST['data']);
foreach($splits as $id => $item) {
    preg_match('/(.*?)=(.*)/',$item, $matches);
    $parts[$matches[1]][] = $matches[2];
}
print_r($parts);

This will give you an array with keys as 1-Column, 2-Column as so on which will contain an array of related value. You can print this array in whatever way you want.

Here is the output:

[1-Column] => Array
    (
        [0] => host1.colo.sub
        [1] => host1.colo.sub
        [2] => host1.colo.sub
    )

[2-Column] => Array
    (
        [0] => Fri Aug 13
        [1] => Fri Aug 13
        [2] => Fri Aug 13
    )

[3-Column] => Array
    (
        [0] => db9.nfl.colo2.
        [1] => pdb2.colo2.
        [2] => gl3_lvm
    )

[4-Column] => Array
    (
        [0] => 00:00:03
        [1] => 04:00:02
    )

[5-Column] => Array
    (
        [0] => 01:55:02
        [1] => 04:48:06
    )

[6-Column] => Array
    (
        [0] => 87.24 MB
        [1] => 54.64 MB
    )

[7-Column] => Array
    (
        [0] => Success
        [1] => Success
    )
aadravid
Thanks @aadravid. Is 'data' my $Inputfile? How do I pass in the text file? thanks.
cjd143SD
your data can be anything... just replace $_POST['data'] with the variable you have read your data into. You can use file("test.txt") as suggested by timdev to read your file.
aadravid
your greedy quantifier (.*) will cause the first match to match the whole rest of the string as well. And while we're at it, better to get rid of the lazy quantifier as well. Better to use negative character classes /([^=]*)=([^;]*)/
Crayon Violent
ok... i have just started learning regular expressions so this would help... thanks for the tip Crayon Violent.
aadravid
+2  A: 
$InputFile = file("test.txt");
foreach ($InputFile as $line){
  preg_match('~(1-Column[^;]*;?)?(2-Column[^;]*)?;?(3-Column[^;]*)?;?(4-Column[^;]*)?;?(5-Column[^;]*)?;?(6-Column[^;]*)?;?(7-Column[^;]*)?;?~',$line,$pieces);
  $pieces = array_pad($pieces,8,'');
  echo "<pre>";print_r($pieces);echo "</pre>";

}

output:

Array
(
    [0] => 1-Column=host1.colo.sub;2-Column=Fri Aug 13;3-Column=db9.nfl.colo2.;4-Column=00:00:03;5-Column=01:55:02;6-Column=87.24 MB;7-Column=Success;
    [1] => 1-Column=host1.colo.sub;
    [2] => 2-Column=Fri Aug 13
    [3] => 3-Column=db9.nfl.colo2.
    [4] => 4-Column=00:00:03
    [5] => 5-Column=01:55:02
    [6] => 6-Column=87.24 MB
    [7] => 7-Column=Success
)

Array
(
    [0] => 1-Column=host1.colo.sub;2-Column=Fri Aug 13;3-Column=pdb2.colo2.;4-Column=04:00:02;
    [1] => 1-Column=host1.colo.sub;
    [2] => 2-Column=Fri Aug 13
    [3] => 3-Column=pdb2.colo2.
    [4] => 4-Column=04:00:02
    [5] => 
    [6] => 
    [7] => 
)

Array
(
    [0] => 1-Column=host1.colo.sub;2-Column=Fri Aug 13;3-Column=gl3_lvm;5-Column=04:48:06;6-Column=54.64 MB;7-Column=Success;
    [1] => 1-Column=host1.colo.sub;
    [2] => 2-Column=Fri Aug 13
    [3] => 3-Column=gl3_lvm
    [4] => 
    [5] => 5-Column=04:48:06
    [6] => 6-Column=54.64 MB
    [7] => 7-Column=Success
)
Crayon Violent
@Crayon Violent - this is cool. thanks! preg_match seems to be a better fit. This is exactly what I am envisioning.
cjd143SD
@Crayon Violent - what if I want to remove the number prefixes for each column? from '1-Column' -> 'Column', '2-Column' -> 'Column'. Can we stick with preg_match? thanks.
cjd143SD
yup. can just move the n- outside of the captured group (putting it in its own non-captured optional group) preg_match('~(?:1-)?(Column[^;]*;?)?(?:2-)?(Column[^;]*)?;?(?:3-)?(Column[^;]*)?;?(?:4-)?(Column[^;]*)?;?(?:5-)?(Column[^;]*)?;?(?:6-)?(Column[^;]*)?;?(?:7-)?(Column[^;]*)?;?~',$line,$pieces);
Crayon Violent
Thanks! worked nicely.
cjd143SD