【发布时间】:2012-02-16 21:24:48
【问题描述】:
我正在编写一个 php 代码来搜索文本文件中的不同变量....
数据在平面文件中逐行列出,数据列表格式为:
date | time | ip | geo-location (city) | //url
数据保存为日志文件('track-log.txt')
我目前的代码是:
$log_file = file_get_contents('track-log.txt');
$log_lines = explode(PHP_EOL, $log_file);
$log_lines = array_flip($log_lines);
unset($log_file);
这会将文本文件分成几行,然后将这些行向后翻转,以便它们在数组 $log_lines[*] 中作为文本文件的最后一行列出,首先显示为 $log_lines[0]
我需要计算有多少次“日期”是相同的......
<..... lots of logs here .... then .....>
jan 1st 2012 | data.....
jan 1st 2012 | data ....
jan 1st 2012 | data ....
jan 1st 2012 | data ....
jan 1st 2012 | data ....
jan 2nd 2012 | data ....
jan 2nd 2012 | data ....
jan 2nd 2012 | data .... <end log>
想象这是日志的结尾......我想:
$count[0] = 3 // last 3x dates are the same
$count[1] = 5 // the 5x dates before that are the same
所以我可以使用
echo $count[0];
在日志的“日期”部分显示最新值的数量。
我希望数组 $count[*] 停止列出 @ 7 个字符串....
$count[0]; ... 最多 ... $count[6]
这将显示最近 7 天的日志页数
....
extra info....日志中每一行的日期格式是
sunday, january 22, 2012 | 16:14:36 | 82.**.***.*** | bolton | //page_url
而且日期格式始终是一样的,因为它是一个脚本,在每个日志行上写入每个日期
....
【问题讨论】:
-
您的启动代码不正确。
array_flip交换键和值。您只需要$lines=file('track-log.txt'); $lines=array_reverse($lines); -
我需要explode命令,逐行分割主输入字符串.....除非你有更好的编码方式......?
-
file()函数为您提供了一系列行。不需要explode()。
标签: php arrays search flat-file