帮助使用 phpexplode() 和组织输出

发布于 2024-09-14 14:41:27 字数 1065 浏览 8 评论 0原文

有人可以告诉我如何正确映射吗?我试图了解如何使用 phpexplode() 并以一种可以以某种有组织的方式检索和打印它们的方式组织这些值。对于每条记录,我想将名称=值放入特定的存储桶中。我每条记录有 (7) 个最大存储桶。有时我的记录无法填满每个桶。

(例如,记录 (2) 缺少属性 (5,6,7),记录 (3) 缺少属性 (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;

到目前为止,我写这个是为了查看我的输出:

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

我想打印出每个属性的相似值,而不是它的混合值。 使用“打印$pieces[3];”

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

单挑你×的.吻 2024-09-21 14:41:27
$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
)
$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
)
野却迷人 2024-09-21 14:41:27

如果您的输入数据非常不规则,您要么需要使用更多代码来解析它,而不仅仅是简单的爆炸,要么准备它,以便它按照爆炸的预期工作。

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

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.

栖迟 2024-09-21 14:41:27

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

请注意,在这里,我在主 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 更改为  或者什么的。

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.

软糖 2024-09-21 14:41:27

看看这是否有帮助...

$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
    )

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
    )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文