Hi!
How can I DELETE the last line in a text file?

I really need this for a shoutbox... I want to triger the function through shoutboxs' inputs... so I don't have to login to the server to delete some lines.... 🙂

also Is there any Pro way to get the IP address..?

I've used: $ip=$_SERVER['REMOTE_ADDR'];

but thats not working properly... it shows one IP address for every users HIT.... 🙁

thanx in advance...

-GM

    This should work to delete the last line:

    <?php
    
    // load the data and delete the line from the array
    $lines = file('filename.txt');
    $last = sizeof($lines) - 1 ;
    unset($lines[$last]);
    
    // write the new data to the file
    $fp = fopen('filename.txt', 'w');
    fwrite($fp, implode('', $lines));
    fclose($fp);
    
    ?>

    -- lilleman

      Hi Lilleman..

      thanx for answering & I'm sorry for the late reply...

      hey... How can I combine these codes?....

      $message = str_replace("(del_msg)","HERE is your PHP code... I think??",$message);

      (this code should triger the php code that you gave me?? pls tell me how to combine these two code??.. thanx

      //call this code
      <?php

      // load the data and delete the line from the array
      $lines = file('filename.txt');
      $last = sizeof($lines) - 1 ;
      unset($lines[$last]);

      // write the new data to the file
      $fp = fopen('filename.txt', 'w');
      fwrite($fp, implode('', $lines));
      fclose($fp);

      ?>

      thanx in advance...

      -GM

        $last = sizeof($lines) - 1 ;
        unset($lines[$last]); 
        
        // These two lines can be accomplished by using array_pop
        // This will also prevent it from inserting blank lines
        $file  = file('text.txt');
        array_pop($file);
        $fp    = fopen('text.txt','w');
        fwrite($fp, implode('',$file));
        fclose($fp);
        

          Hi LordShryku....

          How do I call this function using the following code, it shouldn't delete the last line from the text file unless I call the php function by a little (secret)msg which trigger the function to delete the last line from the php which is located on the server.

          $message = str_replace("(deleteMsg)","HERE is your PHP code... I think??",$message);

          (deleteMsg)=this is a little secret code that I write in a shoutbox message field.. so when I write this "(deleteMsg)" It shouldn't post a message, it should just call the "delete the last line" function...

          ok..ok... let me show you an example... so you know what I really want..

          Letz say I want to post a smilie picture... I just write on a message field 🙂 and the php code for this is:

          $message = str_replace("🙂","<Img src="smiling.gif">",$message);

          so that code post a picture... I think you got the point... The very same way I want to delete the last file function to be called...

          I just don't know how should I call it... thats it..

          (sorry for the lage explanation..)

          thanx in advance..

          -GM

            Well, you can't call it from str_replace. If I understand you right, I'd probably make it a function, make your trigger a variable, and use a conditional.

            function drop_line() {
               $file  = file('text.txt');
               array_pop($file);
               $fp    = fopen('text.txt','w');
               fwrite($fp, implode('',$file));
               fclose($fp);
            }
            // ................
            $trigger = "deletefile";
            //  ................
            if($message == $trigger) {
               drop_line();
            }

              Hi LordShryku...
              Thankyou man... it really works 😃
              I appreciate your help... 🙂

              see ye around....

              -GM

                Write a Reply...