问个服务器日志的正则怎么写

jwmwd001 2013-07-02 03:17:08
2013-06-23 04:33:51 W3SVC1539885 198.56.185.162 GET /robots.txt - 80 - 66.249.75.65 Mozilla/5.0+(compatible;+Googlebot/2.1;++http://www.google.com/bot.html) 404 0 2

我想分别匹配日期2013-06-23/时间04:33:51/服务器ip198.56.185.162/文件地址robots.txt/蜘蛛ip66.249.75.65/蜘蛛信息Mozilla/5.0+(compatible;+Googlebot/2.1;++http://www.google.com/bot.html)/状态码404 0 2/,这个如何精确匹配?
...全文
256 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzuning 2013-07-05
  • 打赏
  • 举报
回复
日志文件一般都很大 你需要在循环中逐行读取,拆分成数组
jwmwdxyz 2013-07-05
  • 打赏
  • 举报
回复
这个分不能浪费了
jwmwd001 2013-07-03
  • 打赏
  • 举报
回复
引用 3 楼 dream1206 的回复:
我想这个日期的格式应该是固定的,你可以按空格将它们分割,例如
$log = '2013-06-23 04:33:51 W3SVC1539885 198.56.185.162 GET /robots.txt - 80 - 66.249.75.65 Mozilla/5.0+(compatible;+Googlebot/2.1;++http://www.google.com/bot.html) 404 0 2';
var_dump( explode(' ', $log) ); 
/**
array(14) {
  [0]=>
  string(10) "2013-06-23"
  [1]=>
  string(8) "04:33:51"
  [2]=>
  string(12) "W3SVC1539885"
  [3]=>
  string(14) "198.56.185.162"
  [4]=>
  string(3) "GET"
  [5]=>
  string(11) "/robots.txt"
  [6]=>
  string(1) "-"
  [7]=>
  string(2) "80"
  [8]=>
  string(1) "-"
  [9]=>
  string(12) "66.249.75.65"
  [10]=>
  string(72) "Mozilla/5.0+(compatible;+Googlebot/2.1;++http://www.google.com/bot.html)"
  [11]=>
  string(3) "404"
  [12]=>
  string(1) "0"
  [13]=>
  string(1) "2"
}
*/
引用 3 楼 dream1206 的回复:
我想这个日期的格式应该是固定的,你可以按空格将它们分割,例如
$log = '2013-06-23 04:33:51 W3SVC1539885 198.56.185.162 GET /robots.txt - 80 - 66.249.75.65 Mozilla/5.0+(compatible;+Googlebot/2.1;++http://www.google.com/bot.html) 404 0 2';
var_dump( explode(' ', $log) ); 
/**
array(14) {
  [0]=>
  string(10) "2013-06-23"
  [1]=>
  string(8) "04:33:51"
  [2]=>
  string(12) "W3SVC1539885"
  [3]=>
  string(14) "198.56.185.162"
  [4]=>
  string(3) "GET"
  [5]=>
  string(11) "/robots.txt"
  [6]=>
  string(1) "-"
  [7]=>
  string(2) "80"
  [8]=>
  string(1) "-"
  [9]=>
  string(12) "66.249.75.65"
  [10]=>
  string(72) "Mozilla/5.0+(compatible;+Googlebot/2.1;++http://www.google.com/bot.html)"
  [11]=>
  string(3) "404"
  [12]=>
  string(1) "0"
  [13]=>
  string(1) "2"
}
*/
但是服务器日志不是每行都是这样的,有很多#开头的,所以才想做个正则过滤掉其他格式的。
改一下昵称 2013-07-02
  • 打赏
  • 举报
回复
我想这个日期的格式应该是固定的,你可以按空格将它们分割,例如
$log = '2013-06-23 04:33:51 W3SVC1539885 198.56.185.162 GET /robots.txt - 80 - 66.249.75.65 Mozilla/5.0+(compatible;+Googlebot/2.1;++http://www.google.com/bot.html) 404 0 2';
var_dump( explode(' ', $log) ); 
/**
array(14) {
  [0]=>
  string(10) "2013-06-23"
  [1]=>
  string(8) "04:33:51"
  [2]=>
  string(12) "W3SVC1539885"
  [3]=>
  string(14) "198.56.185.162"
  [4]=>
  string(3) "GET"
  [5]=>
  string(11) "/robots.txt"
  [6]=>
  string(1) "-"
  [7]=>
  string(2) "80"
  [8]=>
  string(1) "-"
  [9]=>
  string(12) "66.249.75.65"
  [10]=>
  string(72) "Mozilla/5.0+(compatible;+Googlebot/2.1;++http://www.google.com/bot.html)"
  [11]=>
  string(3) "404"
  [12]=>
  string(1) "0"
  [13]=>
  string(1) "2"
}
*/
jwmwd001 2013-07-02
  • 打赏
  • 举报
回复
引用 1 楼 hnxxwyq 的回复:
最好举一个特定的例子,然后给出你期望的结果,你的问题表示看不懂
代码就是特定的例子,我想取的值标注在下面,就是想写一句话正则匹配,用pregmatch这种生成一个数组然后我再干点其他的事。
lazyboy_wu 2013-07-02
  • 打赏
  • 举报
回复
最好举一个特定的例子,然后给出你期望的结果,你的问题表示看不懂

21,893

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧