string matching from .txt file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lizzardnub
Forum Newbie
Posts: 3
Joined: Sat Jun 24, 2006 3:19 pm

string matching from .txt file

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
lizzardnub
Forum Newbie
Posts: 3
Joined: Sat Jun 24, 2006 3:19 pm

ungreedy search strings

Post 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.
Post Reply