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.
extracting line containing an IP
Moderator: General Moderators
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>\d{1,3} one to three digits
\. a dot (escaping the special meaning of . in a pattern)
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.
I'm not a herro in regular expressions but maybe somebody else is.
Code: Select all
<?php
$str = $_SERVER["REMOTE_ADDR"] . " | " . $_SERVER["HTTP_USER_AGENT"] . "\n";
$fp = fopen("./log.txt", "a+");
fputs($fp, $str);
fclose($fp);
?>Code: Select all
<?php
echo "Your IP is $_SERVER[REMOTE_ADDR]";
?>