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 != "\n") {
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 != "\n") {
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);
}
?>