Home > Backend Development > PHP Tutorial > PHP program to count and sort logs

PHP program to count and sort logs

WBOY
Release: 2016-07-25 09:03:16
Original
947 people have browsed it
  1. function topIp($logfile,$length=3){
  2. $handle = fopen($logfile, 'r');
  3. $countip = array();//statistics ip
  4. if ($handle) {
  5. while ($buffer = fgets($handle)) {//Read the file line by line
  6. $arr = preg_split('/t/',$buffer);
  7. if(strstr($arr[2 ],"small")){//Small picture
  8. //ip is the key, the number of occurrences is
  9. $countip[$arr[1]] = $countip[$arr[1]] ? ++$countip[$ arr[1]] : 1;
  10. }
  11. }
  12. fclose($handle);
  13. arsort($countip);//The number of ip occurrences is in reverse order
  14. return array_slice($countip,0,$length);//Extract
  15. }
  16. }
  17. $topips = topIp('20121030.log',3);
  18. var_dump($topips);
  19. ?>
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) }



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template