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 15 of 15
  1. #1
    Regular Coder
    Join Date
    Oct 2011
    Posts
    113
    Thanks
    9
    Thanked 1 Time in 1 Post

    Delete certain line in txt file

    Hello I am trying to delete a certain line in a txt file using PHP here is an example
    data.txt
    PHP Code:
    Red
    Blue
    Green
    Yellow
    Orange 
    Lets say I want to delete Green
    I want it to delete the line and do NOT leave a blank white space

    End result
    PHP Code:
    Red
    Blue
    Yellow
    Orange 

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,503
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    This goes back to the discussion of using MySQL.

    You're using what is called "flat-file" database.
    Everything you do (add, edit, delete) will involve several lines of code.
    Way more work than MySQL, but it is what it is.

    Something like below ... but not exactly ... just to give you an idea.

    PHP Code:

    // you already have an array from previous read of the file.
    // pretend your array is called,  array1.

    // let's say you want to delete "Red".
    $delete="Red";

    // open up your database (file) to write ... this is going to overwrite your file ...
    $filename="data.txt";
    $fh fopen($filename'w') or die("can't open file");

    $count_array=count($array1);
    for (
    $i=0;$i<$count_array;$i++) {
    $temp=explode("|",$array1[$i]);
    $color=$temp[0];
    $url=$temp[1];

    if(
    $delete){
    if(
    $delete == $color){
    // do nothing
    // this is the one to delete, so don't write this one to the file.
    }
    else{
    fwrite($fh"$color|$url|\n");
    }
    }
    // if delete

    }//for loop
    fclose($fh); 

  • Users who have thanked mlseim for this post:

    Vernk (10-02-2012)

  • #3
    Regular Coder
    Join Date
    Oct 2011
    Posts
    113
    Thanks
    9
    Thanked 1 Time in 1 Post
    Looks like that code is deleting everything

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,503
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Change this:
    if($delete == $color){

    To this:
    if($delete === $color){

    Not sure why that should make a difference, but see if it does.

  • #5
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    This will work if you're looking for a specific value (ie: Green):
    PHP Code:
    // Grab the file's rows in an array
    $rows file'./data.txt' );

    // look for `Green` (we trim all values because trailing line breaks, etc, are included)
    $key array_search'Green'array_map'trim'$rows ) );

    // Does the key exist? Remove it and rewrite the data
    if ( FALSE !== $key )
    {
        unset( 
    $rows$key ] );
        
    file_put_contents'./data.txt'implode''$rows ) );

    This will work if you're looking for a specific line number:
    PHP Code:
    // Grab the file's rows in an array
    $rows file'./data.txt' );

    // Does line 3 (offset 2) exist? Remove it and rewrite the data
    if ( isset( $rows[2] ) )
    {
        unset( 
    $rows[2] );
        
    file_put_contents'./data.txt'implode''$rows ) );

    Last edited by kbluhm; 10-02-2012 at 03:13 PM.

  • Users who have thanked kbluhm for this post:

    Vernk (10-02-2012)

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,503
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    yea ... do what kbluhm has.

    my way is not so efficient.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    This won't work though (for what you want): file_put_contents( './data.txt', implode( '', $rows ) );. That will write concurrently on a single line, not over multiple lines so it won't work in the next iteration of a run. Use this for the implode: implode(PHP_EOL, $rows) instead.
    No matter what you do in a file handle, you cannot just yank a line. You need to yank it and overwrite the entire data in the existing file. This is why a database is the best option.

  • #8
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Read the comments... file() will return each line as a whole, including new lines.
    PHP Code:
    // look for `Green` (we trim all values because trailing line breaks, etc, are included)
    $key array_search'Green'array_map'trim'$rows ) ); 
    There is a flag you can pass to file() to avoid this, FILE_IGNORE_NEW_LINES, but I just left them intact for my examples.
    Last edited by kbluhm; 10-02-2012 at 04:20 PM.

  • #9
    Regular Coder
    Join Date
    Oct 2011
    Posts
    113
    Thanks
    9
    Thanked 1 Time in 1 Post
    PHP Code:
    if (isset($_POST['delsite'])) {
    //$sstring = $_POST['ids'];
    $delete $_POST['ids'];
    $rows file('data.txt');

    // look for `Green` (we trim all values because trailing line breaks, etc, are included)
    $key array_search$deletearray_map'trim'$rows ) );

    // Does the key exist? Remove it and rewrite the data
    if ( FALSE !== $key )
    {
        unset( 
    $rows$key ] );
        
    file_put_contents'data.txt'implode''$rows ) );

    header("location:?access");

    It doesn't do anything

  • #10
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    What doesn't it do?

    $_POST['ids']... `ids` is plural which suggests multiple values. We'll need more info than "it doesn't do anything". ;)

  • #11
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by kbluhm View Post
    Read the comments... file() will return each line as a whole, including new lines.
    PHP Code:
    // look for `Green` (we trim all values because trailing line breaks, etc, are included)
    $key array_search'Green'array_map'trim'$rows ) ); 
    There is a flag you can pass to file() to avoid this, FILE_IGNORE_NEW_LINES, but I just left them intact for my examples.
    Oh yes I see what you mean. Since you file without removing the linefeeds, then you still have the linefeeds while imploding.
    I'm not sure I like that.

  • #12
    Regular Coder
    Join Date
    Oct 2011
    Posts
    113
    Thanks
    9
    Thanked 1 Time in 1 Post
    Code:
    data.txt
    1|MinecraftServers.net
    2|somethingelse.com
    3|another.com
    $_POST['ids'] selected from <select name="ids">
    options so after they select from the drop down

    $_POSt['ids'] would be ID|website.com

    So it's trying to delete the line 2|somethingelse.com

  • #13
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    And nor should you expect this would work. Your criteria was scalar; it had no indication of a key | value relationship in the file so you cannot search using something as simple as there is here.

    This now requires you to [s|f]scanf / list / explode the data and separate the "key" out so you can find where it is in the file. If they are always numerically incremented matching the line number, then you can still use $key - 1 as whatever the posted line number is instead of using an array_search. Kbluhm's post in #5 there shows how to do that with just a line number.

    Is there a reason why you don't simply use a db for this? Issuing a delete command would be a lot easier.

  • #14
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,503
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    We all mentioned MySQL.

    I can only assume it's the learning-curve issue that comes with it.

    Most likely there will be more things added to the database, and
    there will be more "updates" to the data. Security, or lack of,
    by using .txt files may also become an issue. We can advise all day
    long, but OP needs to decide if learning MySQL will suit him/her best.

  • #15
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    I went into my "php code vault" and came up with this code I used for spell checking. It uses the browsers spell checking features to check my text before I enter it into the database.

    Create a spellcheck.txt and the html form reads the contents into the text field, you then edit the form and submit to re-write the txt file. I don't have php running to test it, but it was working the last time I used it.

    spellcheck.php
    PHP Code:
    <?php
    if(isset($_POST['update'])) {

    $update $_POST['update'];
    $fh fopen('spellcheck.txt''w') or die("can't open file");
    fwrite($fh$update);
    fclose($fh);

    }
    ?>
    <head>
    <style type="text/css">
    textarea {
    font-size: 24px;
    width: 800px;
    height:500px;
    }
    </style>
    </head>
    <body>
    <center>
    <form method="post" action="spellcheck.php">
    <textarea name="update">
    <?php
    echo file_get_contents("spellcheck.txt");
    ?>
    </textarea><br><br>
    <input type="submit" value="Submit" name="submit">
    </form>
    </center>
    </body>
    </html>


    ----
    Last edited by Len Whistler; 10-02-2012 at 09:09 PM.
    Leonard Whistler


  •  

    Posting Permissions

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