首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP读取文本文件忽略特定选项卡

PHP读取文本文件忽略特定选项卡
EN

Stack Overflow用户
提问于 2018-11-13 01:41:29
回答 1查看 178关注 0票数 0

我有日志文件数据(.txt) 这里

正如您在日志文件中看到的那样,日志文件包含DateTime内容。

现在我有了PHP函数来从日志文件中获取数据。

代码语言:javascript
复制
$fromDateTime = new DateTime('Wed, Sep 19  2018 08:00:00');
$toDateTime = new DateTime('Wed, Sep 19  2018 19:59:00');
$file = file_get_contents('Reject.txt');
$lines = explode("\n", $file);

// counter
$rowsintimespan = 0;
// keys should correspond to columns
$keys = [
    'Date',
    'Time',
    'Battery level',
    'Piezo sound level',
    'Left (Channel 3) light intensity',
    'Right (Channel 1) light intensity'
];

$values = array_fill(0, count($keys), 0);
$values = array_combine($keys, $values);

// Do Line-By-Line starting by Line 16 (Array Index 15)
for ($i = 11; $i < count($lines); $i++) {
    // if the file is "Tue, Sep 18<tab>2018<tab>23:59:53<tab>"
    $dateobj = DateTime::createFromFormat("???,?M?d??Y?H:i:s+", $lines[$i]);

    // check if date is in your Timespan
    if ($dateobj < $toDateTime && $dateobj > $fromDateTime) {
        $rowsintimespan++; // count if in timespan

        // get line elements
        $lineContent = explode("\t", $lines[$i]);

        // loop through line elements and count them
        $x = 0;
        for ($j = 0; $j < count($keys); $j++) {
            if (!isset($lineContent[$j])) {
                continue;
            }

            // remember position of last not empty column
            if (trim($lineContent[$j]) != '') {
                $x = $j;
            }
        }

        if ($x > 0) {
            $values[$keys[$x]]++;
        }
    }
}

// Debug-Output
echo $rowsintimespan;

// Output every column
echo '<pre>';
print_r($values);

现在我不想要DateTime,包括计数在内。因此,我尝试将两者都删除,但计数的结果并不在正确的keys中。

我的问题是,是否可以忽略这两个键,然后计数是正确的?

在上面的图片中,我尝试删除DateTime

正确的应该是,Battery level = 2Piezo sound level = 2

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-13 02:50:08

您应该减去列数:$values[$keys[$x]]++;$values[$keys[$x - 2]]++;

下面是更新的代码片段:

代码语言:javascript
复制
<?php
$fromDateTime = new DateTime('Tue, Nov 13  2018 08:00:00');
$toDateTime = new DateTime('Tue, Nov 13  2018 19:59:00');
$file = file_get_contents('reject_new.txt');
$lines = explode("\n", $file);

const IGNORE_COLS = 2;  // +++ Introduced const for number of columns to skip

// counter
$rowsintimespan = 0;
// keys should correspond to columns
$keys = [
    // 'Date',         // +++ 
    // 'Time',         // +++ 
    'Battery level',
    'Piezo sound level',
    'Left (Channel 3) light intensity',
    'Right (Channel 1) light intensity'
];

$values = array_fill(0, count($keys), 0);
$values = array_combine($keys, $values);

// Do Line-By-Line starting by Line 12 (Array Index 11)
for ($i = 11; $i < count($lines); $i++) {
    // if the file is "Tue, Sep 18  2018<tab>23:59:53<tab>"
    $dateobj = DateTime::createFromFormat("???,?M?d??Y?H:i:s+", $lines[$i]);

    // check if date is in your Timespan
    if ($dateobj < $toDateTime && $dateobj > $fromDateTime) {
        $rowsintimespan++; // count if in timespan

        // get line elements
        $lineContent = explode("\t", $lines[$i]);

        // loop through line elements and count them
        for ($j = IGNORE_COLS; $j < count($keys); $j++) {
            if (!isset($lineContent[$j])) {
                continue;
            }

            if (trim($lineContent[$j]) != '') {
                $values[$keys[$j - IGNORE_COLS]]++;   // −−− Remove redundant $x var
            }
        }
    }
}

// Debug-Output
echo $rowsintimespan . PHP_EOL;

// Output every column
echo '<pre>' . PHP_EOL;
print_r($values);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53272556

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档