Log in

View Full Version : Resolved compare exact word/number from array to per line in file



vivien
05-09-2011, 06:52 AM
:confused:just a newbie in php
this code really works, but not that exact, it can really compare word from array to per line in a file. but what im trying to do is, to compare the exact word/number.

if(strstr($line,$key))
echo $line;

if $key = 123 and line 1 is 1234 and line 2 is 123
it will still output line1



i tried to change it from if(strstr($line,$key)) to if($line == $key) and to if(strcmp($line,$key) but it still wont work





<?
$key = "waka";
$fc = file("file.txt");
$f = fopen("file.txt","r");

foreach($fc as $line)
{
if(strstr($line,$key)
echo 4line;
}
fclose($f);
?>

vivien
05-09-2011, 07:03 AM
ok. i think i solved it. i first explode the line and then compare it.

midhul
05-09-2011, 07:08 AM
strcmp() should work.

The fgets() function is used to fetch the data of a file line by line:



$file = fopen("file.txt", "r") or exit("Unable to open file!");
$key = "waka";
$count = 0;
while(!feof($file))
{

$line = explode(" ", fgets($file));

if (strcmp($line[$count], $key)) {
echo $line[$count]. "<br />";
}
count++;
}
fclose($file);