PHP fopen() / fwrite() help

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    PHP fopen() / fwrite() help

    How to remove multiple lines in txt file using fwrite/fopen method? This is my .txt file

    Code:
    One
    Two
    Three
    One
    Four
    Five
    Three
    Eight
    I want to remove those repeated lines (One & Three)

    Help me, thanks!

    #2
    I think, you can't do it directly with only fopen & fwrite.
    You have to get whole file into an array, clean repeated items and overwrite whole with new data.

    Comment


      #3
      I don't really understand what you want to achieve but...

      Code:
      $chop_lines = array(4, 7);
      
      $file = "path/to/file.txt";
      
      $data = file_get_contents($file);
      
      $data_array = explode("\n", $data);
      
      foreach ($chop_lines as $line)
      {
          $line = (int) $line - 1;
      
          if(isset($data_array[$line])) unset($data_array[$line]);
      }
      
      $data = implode("\n", $data_array);
      
      file_put_contents($file);
      If on the other hand u wanna keep only unique entries, u could probably do something like...

      Code:
      $file = "path/to/file.txt";
      
      $data = file_get_contents($file);
      
      $data_array = explode("\n", $data);
      
      $data_array = array_unique($data_array);
      
      $data = implode("\n", $data_array);
      
      file_put_contents($file);
      Last edited by CreativityKills; 25.03.13, 13:32.

      Comment


        #4
        I suggested this
        Originally posted by CreativityKills View Post

        Code:
        $file = "path/to/file.txt";
        
        $data = file_get_contents($file);
        
        $data_array = explode("\n", $data);
        
        $data_array = array_unique($data_array);
        
        $data = implode("\n", $data_array);
        
        file_put_contents($file);

        Comment

        Working...
        X