drk49438 2018-11-13 01:41
浏览 12
已采纳

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

I have log file data(.txt) here

As you can see on log file that has Date and Time content.

And now I have PHP function to get the data from log file.

$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("
", $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);

And now I don't want Date and Time including on counting. So I try to remove both, but the result of count is not in the correct keys.

My question, is it possible to ignore both keys then the counting is keep correct?

enter image description here

Above image, I try to removed Date and Time.
The correct one should be, Battery level = 2 and Piezo sound level = 2.

  • 写回答

1条回答 默认 最新

  • dream_high1026 2018-11-13 02:50
    关注

    You should subtract the number of columns: $values[$keys[$x]]++;$values[$keys[$x - 2]]++;

    Here is an updated code snippet:

    <?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("
    ", $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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题