Results 1 to 3 of 3

Thread: compare exact word/number from array to per line in file

  1. #1
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post compare exact word/number from array to per line in file

    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



    Code:
     
    <?
    $key = "waka";
    $fc = file("file.txt");
    $f = fopen("file.txt","r");
    
    foreach($fc as $line)
    {    
           if(strstr($line,$key)   
              echo 4line;
    }
    fclose($f);
    ?>
    Last edited by vivien; 05-09-2011 at 07:04 AM.

  2. #2
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Talking

    ok. i think i solved it. i first explode the line and then compare it.

  3. #3
    Join Date
    Feb 2008
    Posts
    81
    Thanks
    8
    Thanked 5 Times in 5 Posts

    Default

    strcmp() should work.

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

    PHP Code:
    $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); 
    Last edited by midhul; 05-09-2011 at 07:13 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •