Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    May 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help deleting line from txt log

    Hello i have Zero knowledge of PHP but unfortunately i need a small code as follow.

    Let us consider i have list of names in a text log something called as file.log

    That file.log contains the following data.

    100.00
    200.500
    49.50
    4.50
    7.88
    What i need is , i need a php code with a text box and captcha , when user enter a value in a text box for ex200.500) and need to give captcha after that need to click delete then that php script need to search that file.log and need to delete that line which contains that text(200.500), can some one please do it for me i already tried in internet becz of zero knowledge i am unable to understand it.

  • #2
    Regular Coder
    Join Date
    Sep 2011
    Posts
    428
    Thanks
    18
    Thanked 26 Times in 26 Posts
    Are the lines all unique (no repeats) or can there be multiple values? If there's multiples, do you want the first/last or all of them removed?

    This could help you:
    PHP Code:

    $input 
    '200.500';

    $file 'file.log';

    removeLine($file$input);

    #Add a line (if needed)
    function addLine($file$input)
    {
        
    $data file_get_contents($file);
        
    $data .= $input."\n";
        
    file_put_contents($file$data);
    }
    #Remove a line
    function removeLine($file$input)
    {
        
    $data file_get_contents($file);
        
    $data explode("\n"$data);
        
    $data array_diff($data, array($input));
        
    $data implode("\n"$data);
        
    file_put_contents($file$data);

    The code above will add inputs to the end of the file and remove any line that matches the input. If you wish to sort the lines when adding then look into the sort() functions of php.

    NOTE: I didn't test these functions so I cannot guarantee they will work, however I'm sure they will since they are pretty simple ones.


  •  

    Posting Permissions

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