Jump to content

PHP searching in txt file error, please help


godsent

Recommended Posts

Then im trying to search some name in txt file it never founds, i think i made a mistake somewhere, but i can't find it.

 

function searchForIt($user)
    {
$pfile = fopen('names.txt',"r");
rewind($pfile);
while (!feof($pfile)) {
	$line = fgets($pfile);
	$tmp = explode(', ', $line);
	if ($tmp[0] == $user) {
		echo "FOUND!";
	} else {
		echo "not found!";
	}
}

    }

 

names file look like this

Winten, Acme, Admin, User, Lover, Super, asap, AU, asd, asdfasdf, asdas, Array, Apache, Sk.Winten, wintenas, wintensk, wintenaaaa, w!nten, 

 

If you know what i did wrong please post,  :-\

Your script was a bit hard to understand because of the use of php4 file read functions. But if you have php5 then the following should work:

$filedata=file_get_contents('names.txt');
$filedata.=' ';
$tmp=explode(', ',$filedata);
foreach($tmp AS $tmp)
{
if ($tmp==$user)
	{
	echo "FOUND";
	$found=1;
	break;
	}
}
if ($found!==1) { echo "not found!";	}
unset($tmp);

This sounds like a good job for XML.

 

this tutorial might help you understand ( http://www.webmonkey.com/tutorial/Get_Started_With_SimpleXML_in_PHP ) if not, there are plenty of ways to do this with php and a txt file as well *nods*

Hard to say without seeing your file. But if you wan to compare all elements in each line, you'd need something like this.

 

function searchForIt($user)
    {
   $pfile = fopen('names.txt',"r");
   rewind($pfile);
   while (!feof($pfile)) {
      $line = fgets($pfile);
      $tmp = explode(', ', $line);
      foreach($tmp AS $key => $value) {
        if ($value == $user) {
           echo "FOUND!";
        } else {
           echo "not found!";
        }
      }
   }
}

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.