文件log.txt:
220.172.12.141 [17/Feb/2011:06:05:52 +0800] "GET /lift.sina.com.cn/tuan.html"
220.172.12.142 [17/Feb/2011:06:05:54 +0800] "GET /lift.sina.com.cn/ent.html"
220.172.12.143 [17/Feb/2011:06:05:55 +0800] "GET /lift.sina.com.cn/tuan.html"
220.172.12.144 [17/Feb/2011:08:05:56 +0800] "GET lift.sina.com.cn/sports.html"
220.172.12.145 [17/Feb/2011:10:05:56 +0800] "GET lift.sina.com.cn/news.html"
220.172.12.145 [17/Feb/2011:11:05:57 +0800] "GET lift.sina.com.cn/tuan.html".
要求用php程序统计出各小时/lift.sina.com.cn/tuan.html 的页面访问次数<?php
$hours = array();
$handle = fopen('log.txt','r+');
if ($handle){
while (!feof($handle)){
$line = fgets($handle,4096);
preg_match('/\d*\.\d*\.\d*\.\d* \[.*:(.*):.*:.* \+0800\] .*"GET\s\/lift\.sina\.com\.cn\/tuan\.html"/', $line,$result);
if (isset($result[1])){
$hours[$result[1]]++;
}
}
}
var_dump($hours);
我是用一个正则分组去做,为什么匹配不出时间
很急 很急
220.172.12.141 [17/Feb/2011:06:05:52 +0800] "GET /lift.sina.com.cn/tuan.html"
220.172.12.142 [17/Feb/2011:06:05:54 +0800] "GET /lift.sina.com.cn/ent.html"
220.172.12.143 [17/Feb/2011:06:05:55 +0800] "GET /lift.sina.com.cn/tuan.html"
220.172.12.144 [17/Feb/2011:08:05:56 +0800] "GET lift.sina.com.cn/sports.html"
220.172.12.145 [17/Feb/2011:10:05:56 +0800] "GET lift.sina.com.cn/news.html"
220.172.12.145 [17/Feb/2011:11:05:57 +0800] "GET lift.sina.com.cn/tuan.html".
要求用php程序统计出各小时/lift.sina.com.cn/tuan.html 的页面访问次数<?php
$hours = array();
$handle = fopen('log.txt','r+');
if ($handle){
while (!feof($handle)){
$line = fgets($handle,4096);
preg_match('/\d*\.\d*\.\d*\.\d* \[.*:(.*):.*:.* \+0800\] .*"GET\s\/lift\.sina\.com\.cn\/tuan\.html"/', $line,$result);
if (isset($result[1])){
$hours[$result[1]]++;
}
}
}
var_dump($hours);
我是用一个正则分组去做,为什么匹配不出时间
很急 很急
$res = array();
while($buf = fgets($fp)) {
preg_match('/([^\s]+) \[([^]]+)\]\s"GET \/?([^\s"]+)/', $buf, $r);
if($r[3] == 'lift.sina.com.cn/tuan.html') {
$k = date('YmdH', strtotime($r[2]));
if(! isset($res[$k])) $res[$k] = 0;
$res[$k]++;
}
}
print_r($res);
Array
(
[2011021706] => 2
[2011021711] => 1
)
220.172.12.145 [17/Feb/2011:11:05:57 +0800] "GET lift.sina.com.cn/tuan.html"
中的是
GET lift.sina.com.cn/tuan.html
而不是
GET /lift.sina.com.cn/tuan.html
/lift.sina.com.cn/tuan.html
我没有php 环境,在别人机器上说根本没输出
用在线测试试了试正则,返回是整个结果,而不是分组内容好奇怪
echo $handle ; //看看读出来对不对