-
- function topIp($logfile,$length=3){
- $handle = fopen($logfile, 'r');
- $countip = array();//statistics ip
- if ($handle) {
- while ($buffer = fgets($handle)) {//Read the file line by line
- $arr = preg_split('/t/',$buffer);
- if(strstr($arr[2 ],"small")){//Small picture
- //ip is the key, the number of occurrences is
- $countip[$arr[1]] = $countip[$arr[1]] ? ++$countip[$ arr[1]] : 1;
- }
- }
- fclose($handle);
- arsort($countip);//The number of ip occurrences is in reverse order
- return array_slice($countip,0,$length);//Extract
- }
- }
- $topips = topIp('20121030.log',3);
- var_dump($topips);
- ?>
Copy the code
The output result:
array(3) { ["192.168.1.110"]=> int(10) ["192.168.1.108"]=> int(8) ["192.168.1.120"]=> int(7) }
|