Page 1 of 1

Looking for a function to find a string in a file

Posted: Tue Nov 15, 2005 3:47 pm
by Daisy Cutter
I am trying to make a blocklist for my site, and I've got a text file with several IPs seperated by a newline. I would like to use a function that returns either TRUE if the string (the IP) is found in the file, or FALSE if it is not.

Thanks!

Posted: Tue Nov 15, 2005 3:52 pm
by trukfixer

Code: Select all

$target = $_SERVER['REMOTE_ADDR'];
$string = file_get_contents('/var/www/whatever/your_ip_file.txt');
$iparr = explode('\n',$string);
if(in_array($target,$ip_arr));
{
    $test = true;
}
else
{
   $test = false;
}

Posted: Tue Nov 15, 2005 3:57 pm
by foobar
Yay for the PHP Manual. The besterest resource on the net!

Posted: Tue Nov 15, 2005 4:13 pm
by Daisy Cutter
trukfixer wrote:

Code: Select all

$target = $_SERVER['REMOTE_ADDR'];
$string = file_get_contents('/var/www/whatever/your_ip_file.txt');
$iparr = explode('\n',$string);
if(in_array($target,$ip_arr));
{
    $test = true;
}
else
{
   $test = false;
}
Thanks a lot! That does it's job well. :D

I guess I was looking for the wrong thing, I was in the PHP manual looking for preg_match and stuff.

Posted: Tue Nov 15, 2005 4:41 pm
by timvw

Code: Select all

$string = file_get_contents('/var/www/whatever/your_ip_file.txt');
$iparr = explode('\n',$string);
The shortcut is to use file.

Posted: Tue Nov 15, 2005 8:32 pm
by josh
The efficient way is to use fgets()