douke1891 2015-05-18 18:36
浏览 85

如何从php中的文件向后读取和处理行

I post this topic because I've tried to solve my problem by checking other posts related to the same problem but I couldn't find the same specific thing.

I'm trying to manage a log file, in which I have a long line, I just want some substring from that line (Which I've already solved by using strtok, that's not the problem), the main problem is about the how to manage the lines.

I need to know if a ID is repeated before, because I want to know if a router hotspot is up. I need to read from the bottom upwards (reverse order I mean) and take one line, from that line I need to take the time (hour) and find in reverse (upwards) in the lines before while that hour is higher thant the current time minus 2 hours, I mean, I need to know if a hotspot has answered in the last two hours, so I need to take a line and find if that id has been viewed before.

I tried this code, but it doesn't work, it's PHP, hope some can help me, thanks.

<?php
// full path to text file
define("TEXT_FILE", "C:\Users\Alvaro\Documents\NetBeansProjects\ManejaLog\log2.txt");
// number of lines to read from the end of file
define("LINES_COUNT", 500);


function read_file($file, $lines) {
    //global $fsize;
    $handle = fopen($file, "r");
    $linecounter = $lines;
    $pos = -2;
    $beginning = false;
    $text = array();
    while ($linecounter > 0) {
        $t = " ";
        while ($t != "
") {
            if(fseek($handle, $pos, SEEK_END) == -1) {
                $beginning = true; 
                break; 
            }
            $t = fgetc($handle);
            $pos --;
        }
        $linecounter --;
        if ($beginning) {
            rewind($handle);
        }
        // Estoy en una linea
            //Atributos de cada linea (DIA/HORA/NASID).
            $nada = strtok($handle,":");
            $dia = strtok(" ");
            $hora = strtok("-");
            $nada2 = strtok(" ");
            $id = strtok(",");
            $cadena = $dia." ".$hora." ".$id;

            //Hora actual, a la que restamos 2, para comprobar los actualizados hasta 2 horas.
            $now = new DateTime();
            $now->format("Y-m-d H:i:s");
            $now->modify("-2 hours");
            //Se busca hacia arriba en el fichero mientras que el valor de horascomprobar <= currentline.gethora (pseucodigo)
            $horascomprobar = $now->format('H');

            $date = new DateTime(dia." ".hora);
            $date->format("Y-m-d H:i:s");
            $horaslinea = $date->format('H');

            while (horascomprobar <= horaslinea) {
                $t = " ";
                while ($t != "
") {
                    if(fseek($handle, $pos, SEEK_END) == -1) {
                        $beginning = true; 
                        break; 
                    }
                    $t = fgetc($handle);
                    $pos --;
                }
                $linecounter --;
                if ($beginning) {
                    rewind($handle);
                }

                //Estoy en una linea
                    //Atributos de cada linea (DIA/HORA/NASID).
                    $auxnada = strtok($handle,":");
                    $auxdia = strtok(" ");
                    $auxhora = strtok("-");
                    $auxnada2 = strtok(" ");
                    $auxid = strtok(",");
                    $datelinea = new DateTime(auxdia." ".auxhora);
                    $datelinea->format("Y-m-d H:i:s");
                    $horaslinea = $datelinea->format('H');
                    if(strnatcasecmp ($id, $auxid)) {
                        echo $cadena;
                    }
                //
                if ($beginning) break;
            }
        //
        //$text[$lines-$linecounter-1] = fgets($handle);
        if ($beginning) break;
    }
    fclose ($handle);
    return array_reverse($text);
}
 ?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测