繁体   English   中英

PHP:从txt文件中获取单个值

[英]PHP: Get single value from txt file

我有这个文本文件:

STATIONS_ID;MESS_DATUM;  QN;PP_10;TT_10;TM5_10;RF_10;TD_10;eor
   5371;201912250000;    2;  903.5;   2.3;   2.2; 100.0;   2.3;eor
   [...]
   5371;201912251820;    2;  913.3;   0.4;   0.3; 100.0;   0.4;eor

我只想得到时间戳为 25-12-2019 18:20 (201912251820) 的最后一行。

这就是我的 php 代码(最后我想将数据保存在我的 MySQL 数据库中):

$row = 1;
if(($handle = fopen("produkt_zehn_now_tu_20191225_20191226_05371.txt", "r")) !== FALSE) 
{
    while(($data = fgetcsv($handle, 1000, ",")) !== FALSE) 
    {
        echo "<p> 1 fields in line $row: <br /></p>\n";
        $row++;

        for($c=0; $c < 1; $c++) 
        {
            echo $data[$c] . "<br />\n";

            $daten = $data[$c];
            $carray = explode(";", $daten);
            list($station,$zeit,$quali,$luftdruck,$temp2m,$temp5cm,$feuchte,$taupunkt) = $carray;

            echo "<b>temp2m:".$temp2m."</b>";

            /*
            $res_wetterdaten_dwd = $sql_datenbank -> query("INSERT INTO wetterdaten_dwd 
            (daten_zeit, daten_luftdruck, daten_temp2m, daten_temp5cm, daten_feuchte, daten_taupunkt, daten_station)
            VALUES ('".$zeit."', ".$luftdruck.", ".$temp2m.", ".$temp5cm.", ".$feuchte.", ".$taupunkt.", ".$station.")");
            */
        }
    }
    fclose($handle);
}
    <?php
function getDateFromFile($file = 'text.txt')
{
    $f = fopen($file, 'r');

    $cursor = -1;
    $line = '';
    fseek($f, $cursor, SEEK_END);
    $char = fgetc($f);

    /**
     * Trim trailing newline chars of the file
     */
    while ($char === "\n" || $char === "\r") {
        fseek($f, $cursor--, SEEK_END);
        $char = fgetc($f);
    }

    /**
     * Read until the start of file or first newline char
     */
    while ($char !== false && $char !== "\n" && $char !== "\r") {
        /**
         * Prepend the new char
         */
        $line = $char . $line;
        fseek($f, $cursor--, SEEK_END);
        $char = fgetc($f);
    }

    $value = explode(";", $line);

    $year = substr($value[1], 0, 4);
    $month = substr($value[1], 4, 2);
    $day = substr($value[1], 6, 2);
    $hour = substr($value[1], 8, 2);
    $min = substr($value[1], 10, 2);

    $date = new DateTime("$year-$month-$day $hour:$min");`
    return $date;
}

print_r(getDateFromFile("produkt_zehn_now_tu_20191225_20191226_05371.txt"));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM