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

php中文网
发布: 2016-06-23 14:15:09
原创
1217人浏览过

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/,这个如何精确匹配?


回复讨论(解决方案)

最好举一个特定的例子,然后给出你期望的结果,你的问题表示看不懂

最好举一个特定的例子,然后给出你期望的结果,你的问题表示看不懂
代码就是特定的例子,我想取的值标注在下面,就是想写一句话正则匹配,用pregmatch这种生成一个数组然后我再干点其他的事。

我想这个日期的格式应该是固定的,你可以按空格将它们分割,例如

$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"}*/
登录后复制

我想这个日期的格式应该是固定的,你可以按空格将它们分割,例如

$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"}*/
登录后复制


我想这个日期的格式应该是固定的,你可以按空格将它们分割,例如
$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"}*/
登录后复制

但是服务器日志不是每行都是这样的,有很多#开头的,所以才想做个正则过滤掉其他格式的。

这个分不能浪费了

日志文件一般都很大
你需要在循环中逐行读取,拆分成数组

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号