SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot Daniel .S.'s Avatar
    Join Date
    Jun 2004
    Location
    Pocklington, UK
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrow Deleting specific file content

    Hi,
    I posted on another forum asking how I would delete specific file content for my IP ban script from a text file that was in a list e.g.

    111.1.1.1
    222.2.2.2
    333.3.3.3

    etc.

    Someone replied and said:

    "First read the txt file into an array.
    then delete the desired variable from the array.
    then write the text file again."

    Is this the best way to do it? If so can you tell me how I would do that.

    Thanks,
    From Daniel .S.
    www.danielsefton.com
    My personal resume with tools, resources, tutorials and my work.

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i suggest to use fopen() and preg_match() to select and delete the selection!"
    cheers

  3. #3
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    // read entire file into array 
    $lines file("oldfile");
    // select lines that do not match pattern
    $selected preg_grep("~$search~"$linesPREG_GREP_INVERT);
    // open new file
    $fp fopen("newfile""wb");
    // write selected lines
    fwrite($fpimplode("\n"$selected));
    // close file
    fclose($fp); 
    or just so (for Perl geeks only)

    PHP Code:
    fwrite(
      
    fopen('newfile''wb'),
       
    implode("\n",
           
    preg_grep("~$search~"file("oldfile"), PREG_GREP_INVERT)
       )
    ); 

  4. #4
    Obsessive designer Infizi's Avatar
    Join Date
    May 2004
    Location
    North Pole
    Posts
    450
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    actually, the best way to do it is to either use a database, or use a .htacces file
    New Design - PHP Slowly flowing in!

    Infizi Studio's

  5. #5
    SitePoint Zealot Daniel .S.'s Avatar
    Join Date
    Jun 2004
    Location
    Pocklington, UK
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks everyone,

    Steriofrog,
    Do you know how I would intergrate it with a HTML form? Because what I want it to do is delete the selected IP Address in a list/menu form item. So I could select an IP from the list then click a submit button that removes it from the file, then moves the ones below the deleted one up one so it remains a clean list.

    Of course I would have to make the list item read from the text file too...how would I do that?

    Thanks so much for any help, it is very highly appreciated!

    From Daniel .S.
    www.danielsefton.com
    My personal resume with tools, resources, tutorials and my work.


Bookmarks

Posting Permissions

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