Jump to content

Check file


BBGaming

Recommended Posts

I'm trying to find a way to search a text file for a certain key, and there will be one key on each line. I'm hoping it could return 0 if there's no match, and 1 if there is.

 

I'm familiar with this in other languages, but I can't find how in PHP you can read a certain line of a file. I can get most of the code already:

$var=0;
for($i=1;$i<=file_lines();$i+=1)
{
if (file_read_line()=="key")
{
$var=1;
}
}

 

But I'm sure you spot two probmlems, they are file_lines() and file_read_line(). Does anybody know the proper replacements, or a simpler way?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/64281-check-file/
Share on other sites

If im getting right what u wanna do, u can use the following code:

 

$file = file('file.txt');
foreach($file as $line){
    if(strstr($line, $key)){
          echo 'Key founded';
    }
}

 

file() passes all the file lines to the array $file, then in the loop i used strstr() to find if there is a $key in each line. Hope it is what u were searching for.

Link to comment
https://forums.phpfreaks.com/topic/64281-check-file/#findComment-320468
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.