So I have this script that parses through a file and inserts data into my DB. But now the files I’m looking at have a bunch of random text(basically a key for the rest of the log) before the stuff that I need. So I need to find the timestamp in the file and then operate as I usually would from there, ignoring all the stuff before the timestamp. The logs look like this-
(Blah blah, random stuff)
'2004-05-12 15:45:00',0,0,0,141713,,123.288,122.449,123.2...
And heres my code for once I ‘hit’ the timestamp, storing the values in an array-
// read and store the values in an array
while (($buffer = gzgets($fp, 8192)) !== false)
{
$val[$i] = $buffer;
$i++;
}
$qry = "insert into afeed
set time_stamp='".$val[0]."',
error_value='".$val[1]."',
firstThing='".$val[2]."',
...
otherstuff='".$val[12]."',
lastThing='".$val[13]."'";
}
So either look for timestamp and start from there, or I was thinking that since all the useless stuff in the beginning is the same every time I could find out the size of it and ‘skip that many bytes’?
Any of this possible?
Both of your potential solutions are possible. The “skip that many bytes” solution would probably be the most straightforward. If you know ahead of time that the length of the useless stuff is going to be, for example, 64 bytes at the beginning of every file, you can use something like
gzseek($fp, 64)to move the file pointer past the useless stuff.Since you said the useless stuff is the same every time, this should work. If the useless stuff is not a predetermined length, this method could still be used once you determined how long the useless stuff is (I’d need to see what the useless stuff is to decide on the best method for doing this).