Results 1 to 6 of 6

Thread: Deleting only a certain line from a file, how??

  1. #1
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Deleting only a certain line from a file, how??

    Can anyone tell me how do I delete a certain word from a file?? I was using this script found on http://php.net/file

    PHP Code:
    $key "test";
    $fc=file("some.txt");
    $f=fopen("some.txt","w");
    foreach(
    $fc as $line){
    if (!
    strstr($line,$key))
    fputs($f,$line);
    }
    fclose($f);

    the problem with this code is it deletes all the lines related to the key. For e.g if the key is test it deletes test1 test2 test3 and every word that has the key.

    I need a script that deletes only the exact key supplied.

    Hope that makes sense.Any help would be appreciated. Thanks.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    If what is the exact key? The line? A word?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Yes a word, something like usernames. The file will look something like this(words will be random, user entered):

    Code:
    test1
    bla
    bla345
    shachi
    twey 
    random word

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    $file = 'some.txt';
    $key = 'twey';
    
    $lines = file($file);
    for($i = 0; $i < count($lines); ++$i)
      if(str_replace("\n", '', str_replace("\r\n", '', $lines[$i])) === $key)
        $lines[$i] = '';
    $f = fopen($file);
    fwrite($f, implode('', $lines));
    fclose($f);
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Twey, it is giving me warnings:

    Code:
    Warning: fopen() expects at least 2 parameters, 1 given in /opt/lampp/htdocs/myprojects/remline.php on line 9
    
    Warning: fwrite(): supplied argument is not a valid stream resource in /opt/lampp/htdocs/myprojects/remline.php on line 10
    
    Warning: fclose(): supplied argument is not a valid stream resource in /opt/lampp/htdocs/myprojects/remline.php on line 11
    But nevermind I solved it myself.

    Thanks for your help twey.

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Sorry, fopen($file, 'w');
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •