Page 1 of 1
extracting line containing an IP
Posted: Tue Jun 03, 2003 9:26 pm
by nigma
Hey, anyone show me how to extract an entire line that contains an IP address?
if I had four lines like this:
Bob had something that I wanted
he then took it to my house
which I sometimes call 127.0.0.1
or home.
I would want to store the third line in a variable.
Posted: Wed Jun 04, 2003 2:12 am
by volka
Code: Select all
<?php
$lines = file('test.data.txt');
$pattern = '!\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b!';
$store = array();
foreach($lines as $line)
{
if (preg_match($pattern, $line))
$store[] = $line;
}
?>
<pre><?php print_r($store); ?></pre>
\b word boundary
\d{1,3} one to three digits
\. a dot (escaping the special meaning of . in a pattern)
Posted: Wed Jun 04, 2003 2:14 am
by []InTeR[]
I think the best way to find this is to use
Regular Expression's.
I'm not a herro in regular expressions but maybe somebody else is.
Posted: Wed Jun 04, 2003 8:43 am
by Drachlen
Code: Select all
<?php
$str = $_SERVER["REMOTE_ADDR"] . " | " . $_SERVER["HTTP_USER_AGENT"] . "\n";
$fp = fopen("./log.txt", "a+");
fputs($fp, $str);
fclose($fp);
?>
I had no idea what volka said, so here is a much less complicated version, you can simply delete the user agent if you want, when visiting this page it logs it to log.txt, if you want it to display it on the page, do something like
Code: Select all
<?php
echo "Your IP is $_SERVER[REMOTE_ADDR]";
?>
But this way it isnt saving it anywhere, its gone once ye exit

Posted: Wed Jun 04, 2003 3:25 pm
by volka
I don't think the user's ip was meant but any line of a (undefined) source that contains one or more IPs somewhere.