1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to loop htrougha file and delete duplicate lines?

Discussion in 'PHP' started by bobby9101, Apr 4, 2007.

  1. #1
    Hi, in a previous post, I asked something about checking if data is in a file, I would like to remove duplicate entries. Hwo on earth would I do that?
    I was thinking nested for loops, on loops through each line and inside another loop would compare each line to the current line.
    Is that what I need to do?
     
    bobby9101, Apr 4, 2007 IP
  2. jitesh

    jitesh Peon

    Messages:
    81
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    1. Use file() function to read a file. this will give the file data in array
    2. Now use array_unique function. This will remove duplicate enntries.
    3. Now write the contents of the array in to file.
     
    jitesh, Apr 4, 2007 IP
  3. klown

    klown Peon

    Messages:
    2,093
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    0
    #3
    From php.net

    
    I created this little function based on the one posted by chasesan at gmail dot com; It find all occurences of a string within another string and returns their positions as an array:
    
    <?PHP
    function findAllOccurences($Haystack, $needle, $limit=0)
    {
      $Positions = array();
      $currentOffset = 0;
      $count=0;
      while(($pos = strpos($Haystack, $needle, $offset)) && ($count < $limit || $limit == 0))
      {
        $Positions[] = $pos;
        $offset = $pos + strlen($needle);
        $count++;
      }
      return $Positions;
    }
    ?>
    I hope this helps someone :)
    
    PHP:
    You should be able to use this to replace all occurrences with nothing, then since it tells you where the occurrences happened you can then add the first occurrence to the nothing you replaced it with.. I hope that makes sense i'm on a php high, also I haven't tested the code posted above.
     
    klown, Apr 4, 2007 IP
    commandos likes this.
  4. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #4
    worked perfectly thanks a bunch
     
    bobby9101, Apr 5, 2007 IP
  5. bahjons

    bahjons Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Sounds great, but I'm a dummy. Can you provide an example script that I can pick apart?

    Thanks for your help.
     
    bahjons, Apr 13, 2007 IP
  6. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #6
    go to php.net, and search for each of those functions... all of it is explained nicely
     
    bobby9101, Apr 13, 2007 IP