douhao6557 2014-05-28 10:30
浏览 137

如何从以下正则表达式PHP中获取值到我的变量中?

I need to parse a big log file to get the ipadress, datetime, source and client from all the records.

The following link with the regex works:

http://regex101.com/r/cG9tY7

Regex:

(?<ipAddress>.*?)\s-\s-\s\[(?<dateTime>.*?)\]\s\"GET\s(?<source>.*?)\"\s[0-9]+\s[0-9]+\s\"-\"\s\"(?<client>.*?)\"

Maybe the format is not ok for PHP ?

I open the file as such:

$filename = "access-streaming_small.log"
$file = popen("tac $filename", 'r');

If I use :

while ($line = fgets($file)) {
echo $line;
}

I get the full log file which is displayed ok.

I need to process it with regex so that I get variables of ipadress, datetime, source and client from all records contained in the log file. Bonus task: I need to filter results to get the most current details of the records generated in the last 10 minutes compared to current time.

  • 写回答

1条回答 默认 最新

  • dtcpvz8162 2014-05-28 10:46
    关注

    Have you tried preg_match? Important are the '/ and /' else it won't work

    preg_match('/(?P<name>\w+): (?P<digit>\d+)/', $str, $matches);

    评论

报告相同问题?

悬赏问题

  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示