Att.: scriptfreaks - Removing empty lines from textfile w/php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Naughty
    Confirmed User
    • Jul 2001
    • 6487

    #1

    Att.: scriptfreaks - Removing empty lines from textfile w/php

    Hi,

    We have a script that is adding new lines for no apparent reason at this time. I want to get rid of the empty lines and re-order the text file every time somebody ads her/his name.

    The file is pretty simple, just lines, like this:
    line1
    ...
    ...
    line2
    ...
    line1001
    ...

    I want to loose the ... lines and have it all nicely formatted, like this
    line1
    line2
    line1001


    I use this script to add lines:
    PHP Code:
            $FileName = $path."/lists/names.php";
            $list = file($FileName);
            $newitem = $_POST["name"]."|^|".$_POST["enterpass"]."|^|".$_POST["enteremail"]."|^|";
            $FilePointer = fopen($FileName, "a+");
            fwrite($FilePointer, "\n".$newitem);
            fclose($FilePointer); 
    
    Who knows how I can change that little piece of code and make it as wanted?

    Thanks,
    Paul
    seks.ai for sale - ping me
  • Naughty
    Confirmed User
    • Jul 2001
    • 6487

    #2
    Thanks :p
    seks.ai for sale - ping me

    Comment

    • rowan
      Too lazy to set a custom title
      • Mar 2002
      • 17393

      #3
      Try putting the \n at the end rather than at the start.

      fwrite($FilePointer, "$newitem\n");

      or

      fputs($FilePointer, "$newitem\n");

      Comment

      • psili
        Confirmed User
        • Apr 2003
        • 5526

        #4
        just a guess, after you run your insert, rebuild the file?

        $file = file_get_contents($FileName);
        $file = preg_replace("\n\n",$file);

        fwrite($FilePointer,$file);

        ---
        eh, nix that. probably an ugly way to do it.
        just get the file fixed and insert correct new lines like rowan said.
        Your post count means nothing.

        Comment

        • SmokeyTheBear
          ►SouthOfHeaven
          • Jun 2004
          • 28609

          #5
          put this as line #4
          $newitem = trim($newitem);

          im new at this so i could be wrong
          hatisblack at yahoo.com

          Comment

          • Naughty
            Confirmed User
            • Jul 2001
            • 6487

            #6
            I did notice it just doubles the amoiunt of rows each time I update, add or edit a line from the file. I'm sure it has to do with /n, but I am still fucking around with it.

            I'll try some of your options as well.
            seks.ai for sale - ping me

            Comment

            • Naughty
              Confirmed User
              • Jul 2001
              • 6487

              #7
              None of the above worked, any other takers?
              seks.ai for sale - ping me

              Comment

              • MickeyG
                Confirmed User
                • May 2004
                • 4134

                #8
                get rid of the \n when your appending to the file it already goes to the end of the file. The only time you need \n is if your appending multiple lines

                Comment

                • Naughty
                  Confirmed User
                  • Jul 2001
                  • 6487

                  #9
                  Originally posted by MickeyG
                  get rid of the \n when your appending to the file it already goes to the end of the file. The only time you need \n is if your appending multiple lines

                  If I do that, this

                  line2
                  line3
                  ...


                  becomes:
                  line2line3
                  ...


                  Using this code:
                  PHP Code:
                          $FileName = $path."/lists/names.php";
                          $list = file($FileName);
                          $list[$line] = $_POST["name"]."|^|".$_POST["newpass1"]."|^|".$email."|^|";
                          $FileName = $path."/lists/newnames.php";
                          $FilePointer = fopen($FileName, "w+");
                          for ($x=0;$x<=$numnames;$x++){
                              fwrite($FilePointer,$list[$x]);//."\n"
                          }
                          fclose ($FilePointer);
                          unlink($path."/lists/names.php");
                          rename($path."/lists/newnames.php",$path."/lists/names.php"); 
                  
                  seks.ai for sale - ping me

                  Comment

                  • Naughty
                    Confirmed User
                    • Jul 2001
                    • 6487

                    #10
                    This works:

                    PHP Code:
                    $FileName = $path."/lists/names.php"; 
                            $list = file($FileName); 
                            $listhahaha91;$linehahaha93; = $_POSThahaha91;"name"hahaha93;."|^|".$_POSThahaha91;"newpass1"hahaha93;."|^|".$email."|^|\n"; 
                            $FileName = $path."/lists/newnames.php"; 
                            $FilePointer = fopen($FileName, "w+"); 
                            for ($x=0;$x<=$numnames;$x++){ 
                                fwrite($FilePointer,$listhahaha91;$xhahaha93;);
                            } 
                            fclose ($FilePointer); 
                            unlink($path."/lists/names.php"); 
                            rename($path."/lists/newnames.php",$path."/lists/names.php"); 
                    
                    seks.ai for sale - ping me

                    Comment

                    Working...