0

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
4

4 回答 4

2
$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>";

}

输出:

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
)
于 2010-08-15T22:45:47.437 回答
0

如果您的输入数据非常不规则,您可能需要使用更多代码来解析它,而不仅仅是简单的分解,或者准备它以使其按预期与分解一起工作。

在第二种情况下,您可以使用strpos查找所有“;” 字符串中的字符并检查它们后面的数字是否按顺序排列。如果一个(或多个)数字被跳过,您应该通过插入另一个“;”来补偿。这种方式 explode 将创建一个空数组元素,并且所有生成的数组都应该对齐。

于 2010-08-15T22:02:46.997 回答
0

虽然尚不清楚您要完成什么,但希望此代码可以帮助您弄清楚发生了什么。

请注意,在这里,我在主 foreach 中添加了另一个循环,以迭代行中的每个名称/值对:

<?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";
        }
    }

}

如果从命令行运行,该脚本将产生漂亮的输出。如果您在浏览器中运行它,您可能希望将 \n 更改为
,并将 \t 更改为或其他内容。

于 2010-08-15T22:03:35.810 回答
0

看看这是否有帮助...

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

这将为您提供一个键为 1-Column、2-Column 的数组,依此类推,它将包含一个相关值的数组。你可以用任何你想要的方式打印这个数组。

这是输出:

[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
    )
于 2010-08-15T22:28:46.663 回答