嘿伙计们,我需要一些关于 php 正则表达式问题的帮助。正则表达式甚至可能不是在这里使用的最佳方法。我根本不知道如何使用 php - 我只是四处浏览示例并尝试将它们粘贴在一起以完成我需要完成的工作。
我当前的代码看起来像这样......
// 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;
}
?>
它基本上读取一个名为 cports.log 的文件并在最后 5 行结尾。但是,我的日志文件示例如下所示...
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}
所以我的 php 文件的输出只是日志文件的最后 5 行。但我需要做的是将每个 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);
?>
这段代码基本上迭代了 5 次,以从 $ip 中打印出 5 个 IP 的地理位置。任何帮助将非常感激!再次,我不知道 php 所以请耐心等待我。:)