[英]Log file php regex issue
Hey guys I need some assistance with a php regex problem. 大家好,我需要有关php regex问题的帮助。 Regex might not even be the best method to use here.
正则表达式甚至可能不是在这里使用的最佳方法。 I do not know how to use php at all - I just surf around looking at examples and try to paste them together to accomplish what I need done.
我根本不知道如何使用php-我只是在浏览示例,然后将它们粘贴在一起以完成我需要做的事情。
My current code looks like this... 我当前的代码如下所示...
// full path to text file
define("TEXT_FILE", "cports.log");
// number of lines to read from the end of file
define("LINES_COUNT", 5);
function read_file($file, $lines) {
$handle = fopen($file, "r");
$linecounter = $lines;
$pos = -2;
$beginning = false;
$text = array();
while ($linecounter > 0) {
$t = " ";
while ($t != "\n") {
if(fseek($handle, $pos, SEEK_END) == -1) {
$beginning = true;
break;
}
$t = fgetc($handle);
$pos --;
}
$linecounter --;
if ($beginning) {
rewind($handle);
}
$text[$lines-$linecounter-1] = fgets($handle);
if ($beginning) break;
}
fclose ($handle);
return array_reverse($text);
}
$fsize = round(filesize(TEXT_FILE)/1024/1024,2);
$lines = read_file(TEXT_FILE, LINES_COUNT);
foreach ($lines as $line) {
echo $line;
}
?>
It basically reads a file named cports.log and tails the last 5 lines. 它基本上会读取一个名为cports.log的文件,并在最后5行结尾。 However an example of my log file looks like this...
但是我的日志文件的示例如下所示:
1/27/2013 7:16:06 PM Added {92.255.176.84}
1/27/2013 7:16:07 PM Removed {92.255.176.84}
1/27/2013 7:16:08 PM Added {176.15.101.53}
1/27/2013 7:16:09 PM Removed {176.15.101.53}
1/27/2013 7:16:23 PM Added {98.119.183.235}
1/27/2013 7:16:24 PM Removed {98.119.183.235}
1/27/2013 7:16:27 PM Added {37.251.51.9}
1/27/2013 7:16:28 PM Removed {37.251.51.9}
1/27/2013 7:16:38 PM Added {92.255.176.84}
1/27/2013 7:16:38 PM Added {82.112.38.83}
1/27/2013 7:16:39 PM Removed {92.255.176.84}
1/27/2013 7:16:39 PM Removed {82.112.38.83}
1/27/2013 7:16:45 PM Added {74.61.121.147}
1/27/2013 7:16:50 PM Removed {74.61.121.147}
So the output of my php file is just the last 5 lines of the log file. 因此,我的php文件的输出仅是日志文件的最后5行。 But what I need done is take each IP nested in the brackets and either store them into an array or a variable to be used in this section of code...
但是我需要做的是将每个IP嵌套在方括号中,然后将它们存储到数组或变量中,以在本节代码中使用...
<?php
require_once("geoipcity.inc");
$ip = "8.8.8.8";
$gi = geoip_open("GeoLiteCity.dat", GEOIP_STANDARD);
$record = geoip_record_by_addr($gi, $ip);
echo "Location One <br>";
echo "Country: " .$record->country_name . "<br>";
echo "City: " .$record->city . "<br>";
echo "Latitude: " .$record->latitude . "<br>";
echo "Longitude: " .$record->longitude . "<br>";
geoip_close($gi);
?>
This block of code is basically iterated 5 times to print out the geolocation of 5 IPs from whatever $ip is. 该代码块基本上被迭代5次,以从$ ip中打印出5个IP的地理位置。 Any help would be much appreciated!
任何帮助将非常感激! Once again I don't know php so please be patient with me.
再一次,我不了解php,请耐心等待。 :)
:)
First tip would be using the file()
function to read a file line-wise, then you can just iterate over them: 第一个技巧是使用
file()
函数按行读取文件,然后可以对其进行迭代:
foreach (file("text.txt") as $line) {
###
}
But using a regex you could read the file at once and have preg_match_all
extract all IPs in one swoop: 但是,使用正则表达式可以一次读取文件,并使
preg_match_all
提取所有IP:
preg_match_all('/\{([\d.]+)\}/', file_get_contents($fn), $matches);
print_r($matches[1]);
With a nicely structured file like that, you can even completely arraytize all columns at once, using 有了这样一个结构良好的文件,您甚至可以使用
#^ ([\d/]+)\s ([\d:]+)\s \w+\s (.*?)\s \{([\d.]+)\}\s* $#mix
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.