我有文本文件上的日志数据(.txt)
文本文件上的数据如下:
City Siren FCT_Tester #1 10039273
Date: 160518, Version: 1.00
ID Test Min Max Unit
1 Battery level 2.85 3.40 V
2 Piezo sound level 1.45 2.80 V
3 Left D3 (Ch 3) light intensity 2000
10000 mcd
4 Right D2 (Ch 1) light intensity 2000
10000 mcd
Date Time Battery level Piezo sound level
Left D3 (Ch 3) light intensity Right D2
(Ch 1) light intensity
Wed, Sep 19 2018 06:47:01 3.372 1.621 9117.000 7303.300
Wed, Sep 19 2018 06:47:19 3.374 1.614 8614.400 7152.300
Wed, Sep 19 2018 08:08:04 3.378 1.604 9586.500 7422.400
Wed, Sep 19 2018 08:08:20 3.375 1.632 9249.300 6682.200
Wed, Sep 19 2018 08:08:44 3.377 1.615 9059.700 6777.100
Wed, Sep 19 2018 08:09:15 3.376 1.626 8902.500 6962.200我所做的是:
echo count(file($files));它会得到所有的行。
所以我现在的问题是,我想从日期和时间算出带有条件的文本文件行数。获取计数Wed, Sep 19 2018 08:00:00,直到Wed, Sep 19 2018 11:00:00
因此,结果应该是:4
有可能吗?
更新
日志文件正在使用选项卡,然后代码可以“用错误读取”:
Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string下面是日志文件
Tue, Sep 18 2018 23:59:53 3.380 1.622 9958.500 7301.000
Wed, Sep 19 2018 00:00:08 3.380 1.622 8817.900 7194.800
Wed, Sep 19 2018 00:00:28 3.343 1.598 9089.500 7033.800
Wed, Sep 19 2018 00:00:45 3.376 1.602 8789.200 7285.200
Wed, Sep 19 2018 00:01:01 3.376 1.629 8406.000 7295.700
Wed, Sep 19 2018 00:01:17 3.378 1.623 8468.100 7382.800
Wed, Sep 19 2018 00:01:36 3.366 1.619 9462.900 7200.600
Wed, Sep 19 2018 00:01:54 3.370 1.622 9389.700 7018.500
Wed, Sep 19 2018 00:02:21 3.375 1.582 9637.100 7347.500
Wed, Sep 19 2018 00:02:36 3.377 1.595 8775.200 7414.700
Wed, Sep 19 2018 00:02:52 3.340 1.585 8955.300 7376.700
Wed, Sep 19 2018 00:03:20 3.263 1.600 8325.900 6694.700
Wed, Sep 19 2018 00:03:37 3.369 1.616 9554.400 7045.700发布于 2018-09-19 07:48:50
你不需要做任何特殊的分裂或重组。只需使用DateTime::createFromFormat并指定您的格式。
如果您的文件是像Tue,<space>Sep<space>18<tab>2018<tab>23:59:53<tab>[...]那样格式化的,您可以使用以下格式的???,?M?d?Y?H:i:s+,该格式可以扩展为
??? -忽略前3个字符(wed、mon等), -逗号? -一些字符(空格,制表符,任意)M -以文本形式的月份(9月、9月等)? -一些字符(空格,制表符,任意)d -作为编号的一天? -一些字符(空格,制表符,任意)Y -年份为四位数(2008年、2009年等)? -一些字符(空格,制表符,任意)Y -24小时内的小时数(09、10、13等): -一个:i -以数字形式记录(09、10、13等): -一个:s -秒为数字(09,10,13等)+ -忽略(但警告!)任何尾随字符所以你的代码看起来就像
//Set Time-Span
$fromDateTime = new DateTime('Wed, Sep 19 2018 00:01:00');
$toDateTime = new DateTime('Wed, Sep 19 2018 00:02:00');
// Load File
$file = file_get_contents('log.txt');
// Split by lines
$lines = explode("\n",$file);
// counter
$rowsintimespan = 0;
// Do Line-By-Line starting by Line 16 (Array Index 15)
for($i = 15; $i < count($lines); $i++) {
// if the file is "Tue,<space>Sep<space>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
}
}
// Debug-Output
echo $rowsintimespan;编辑回复您的评论,提供真实的文件:
通过查看hexdump -C of您的test.txt,您可以发现,在日期和年份之间有两个空格(第一行中的最后一个20 20 )。
00000270 0a 0d 0a 54 75 65 2c 20 53 65 70 20 31 38 20 20 |...Tue, Sep 18 |
00000280 32 30 31 38 09 32 33 3a 35 39 3a 35 33 09 33 2e |2018.23:59:53.3.|
00000290 33 38 30 09 31 2e 36 32 32 09 39 39 35 38 2e 35 |380.1.622.9958.5|
000002a0 30 30 09 37 33 30 31 2e 30 30 30 0d 0a 57 65 64 |00.7301.000..Wed|所以你的格式是???,?M?d??Y?H:i:s+。请注意d??Y之间的第二个d??Y。
发布于 2018-09-19 03:52:15
我不是regex专家,但你可以试试这个
<?php
$content = file_get_contents('b.txt');
$lines = preg_match_all('/(Mon|Tue|Wed|Thu|Fri|Sat|Sun),[ ]{1,}(Jun|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[ ]{1,}[0-9]{2,2}.*/m', $content, $matches);
$fromDateTime = new DateTime('Wed, Sep 19 2018 08:00:00');
$toDateTime = new DateTime('Wed, Sep 19 2018 11:00:00');
foreach($matches[0] as $line){
$cols = explode(' ', $line);
$dateTimeArray = [];
foreach ($cols as $index => $col) {
if ($index < 8 && $col) {
$dateTimeArray[] = $col;
}
}
$dateTime = new DateTime(implode(' ', $dateTimeArray));
if ($dateTime > $fromDateTime && $dateTime < $toDateTime) {
print_r($line."\n");
}
}发布于 2018-09-19 06:36:31
我不知道你到底想要什么,除了逐行获取文本文件,他的代码是他的代码,你可以在其中解析特殊文本的行。
<?php
$file = fopen("welcome.txt", "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached
$counter=0;
while(!feof($file))
{
echo fgets($file). "<br>";
$counter++;
}
fclose($file);
?>https://stackoverflow.com/questions/52397274
复制相似问题