Forum Moderators: coopster

Message Too Old, No Replies

Extract data from a file

         

JamesTrix

1:27 am on Dec 22, 2008 (gmt 0)

10+ Year Member



I am trying to extract a list of IP addresses from a log file with the following google code and not having much luck

<?
$file = file_get_contents('http://www.mysite.com/log.html');
preg_match_all('/^([1-9]¦[1-9][0-9]¦1[0-9][0-9]¦2[0-4][0-9]¦25[0-5])(\.([0-9]¦[1-9][0-9]¦1[0-9][0-9]¦2[0-4][0-9]¦25[0-5])){3}$/',$file,$a);

$count = count($a[1]);
echo "<b>Number of hack attempts ips</b> = " .$count."<p>";
for ($row = 0; $row < $count ; $row++) {
echo $a[1]["$row"]."<br>";
}

?>

The log file looks like

Referer: http://www.example.com/page1.htm, Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1), 203.26.142.253, Sunday 21st of December 2008 07:13:46 PM

Any help on this would be great I am trying to get a list of IPs in this format

127.0.0.1
10.10.10.1
192.168.0.1
ect

[edited by: dreamcatcher at 12:30 pm (utc) on Dec. 22, 2008]
[edit reason] use example.com. Thanks. [/edit]

PHP_Chimp

3:19 am on Dec 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



preg_match_all('/^

means that the number to try and match an ip address needs to be at the beginning of the log file.
If you use preg_match_all('/ no ^ then it will take longer, but look through all of the information on each line.

[edited by: PHP_Chimp at 3:19 am (utc) on Dec. 22, 2008]