-
-
function topIp($logfile,$length=3){
- $handle = fopen($logfile, 'r');
- $countip = array();//统计ip
- if ($handle) {
- while ($buffer = fgets($handle)) {//逐行读取文件
- $arr = preg_split('/\t/',$buffer);
- if(strstr($arr[2],"small")){//小图
- //ip为键,出现次数为指
- $countip[$arr[1]] = $countip[$arr[1]] ? ++$countip[$arr[1]] : 1;
- }
- }
- fclose($handle);
- arsort($countip);//ip出现次数倒序
- return array_slice($countip,0,$length);//提取
- }
- }
- $topips = topIp('20121030.log',3);
- var_dump($topips);
- ?>
复制代码
输出的结果:
array(3) { ["192.168.1.110"]=> int(10) ["192.168.1.108"]=> int(8) ["192.168.1.120"]=> int(7) }
|