Page 1 of 1

string matching from .txt file

Posted: Sat Jun 24, 2006 3:24 pm
by lizzardnub
I was able to match a string such as "[222.222.222.222]" but the goal was to use a .txt file for the string so I tried fopen with no luck.

Code: Select all

<?
$str = fopen( "log/log.txt" , "r" );
$pat = "\[.*\]";
$regs = array();
if(ereg($pat,$str,$regs)) {
    echo "match: {$regs[0]}\n";
}
?>
If someone could point me in the right direction I'd be appreciative.

Posted: Sat Jun 24, 2006 6:02 pm
by feyd
You didn't load anything from the file, just opened it and used it's handle, which is not the data contained inside. file_get_contents() may be of interest.

ungreedy search strings

Posted: Sat Jun 24, 2006 8:08 pm
by lizzardnub
I tried using the >U PCRE modifier I read about at http://www.pcworld.idg.com.au to make the output more like

Code: Select all

[ip], [localhost], [ip]
by using

Code: Select all

$pat = "<\[.*\]>U";
but no joy. So this is where I stand

Code: Select all

<?
$str = file_get_contents( "log/log.txt");
$pat = "\[.*\]";
$regs = array();
if(ereg($pat,$str,$regs))
{
echo "match: {$regs[0]}\n";
}
?>

Code: Select all

[13.249.161.18] 23-06-2006 05:10 [localhost] installed blahblah crap on [8.46.176.244] 23-06-2006 05:10 [localhost] logged in to [8.46.176.244]

The end goal of this little project is to extract a bunch of IP's that are enclosed in brackets out of a text file and put them into a mysql database. I'm hoping to get only one instance of each IP address with the duplicates in the .txt file incrementing some sort of counter next to the IP.