Results 1 to 8 of 8

Thread: Replacing line in text file

  1. #1
    Join Date
    Dec 2005
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Replacing line in text file

    Hi,

    is there any way to replace line in text file? I know how to clear line, but replacing it is problem to me. For example, I have file that looks like this:
    Code:
    lalalall|lalalla|lalla
    kokokok|kokoko|kokoko
    eeeee|eeee|eeee
    and I want to replace line with red color with another text.

    Thanks in advance.

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

    Default

    Code:
    define('LINE_BREAK', "\n"); // \n for *n?x, \r\n for Windows
    
    function replaceLine($fileName, $oldLine, $newLine) {
      $lines = file($fileName);
      for($i = 0; $i < count($lines); ++$i)
        if($lines[$i] == $oldLine)
          $lines[$i] = $newLine;
      $f = fopen($fileName, 'w');
      fwrite($f, implode(LINE_BREAK, $lines));
      fclose($f);
    }
    You might want to also see this thread.
    Last edited by Twey; 07-12-2006 at 07:04 AM.
    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
    Dec 2005
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I tried that, but isn't working...

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

    Default

    Sorry, there was a broken BBCode in it.
    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
    Dec 2005
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I notified, but it weren't working... I solved the problem myself..

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

    Default

    liro: Would you like to share how you fixed it?? It's not a must of course. But I am having the same problem.

  7. #7
    Join Date
    Dec 2005
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by shachi
    liro: Would you like to share how you fixed it?? It's not a must of course. But I am having the same problem.
    Ok, here:
    PHP Code:
    <?php
    //Function for replacing line in text file.
    //Credit: Iiro Krankka
    function replaceLineInTextFile($file$pattern$replacement) {
        if(!
    file_exists($file)) { // if file doesn't exist...
            
    print "The specified file doesn't seem to exist."// ...stop executing code.
        
    } else { // if file exists...
            
    $f file($file); // ...make new variable...
            
    $content// ...and another...

            
    for($i 0$i count($f); $i++) { // ...run through the loop...
                
    if(eregi($pattern$f[$i])) { // and
                    
    $content .= $replacement "\n"// get
                
    } else { // the
                    
    $content .= $f[$i]; // content.
                
    }
            }

            
    $fi fopen($file"w"); // open specified file...
            
    fwrite($fi$content); // and rewrite it's content.
            
    fclose($fi); // close file.

            
    print "Line replaced!!!";
        }
    }

    replaceLineInTextFile("online.txt""1""Found!"); // testing...
    ?>
    I used it this way (variables in Finnish):
    PHP Code:
    <?php 
    function replaceLine($f$etsi$uus) {
        if(!
    file_exists($f)) {
            die(
    "Tiedostoa " $f " ei löytynyt.");
        } else {
        
    $filu file($f);
        
    $sisalto;

            for(
    $i 0$i count($filu); $i++) {
                list(
    $id$nimi$passu) = explode("|"$filu[$i]);

                if(
    $id == $etsi) {
                    
    $sisalto .= $id "|" $uus "\n";
                } else {
                    
    $sisalto .= $filu[$i];
                }
            }

            
    $fi fopen($f"w");
            
    fwrite($fi$sisalto);
            
    fclose($fi);
        }
    }

    replaceLine("online.txt""43534""haha|vaihto!!");
    ?>
    and my online.txt file:
    Code:
    34344|hehe|passu
    43433|hehe|passu
    45342|hehe|passu
    45646|hehe|passu
    43534|haha|passu
    76876|hehe|passu
    76867|hehe|passu
    34534|hehe|passu
    P.S. Do You have Messenger? It would be great to share some scripts....
    Last edited by Iiro; 07-14-2006 at 07:54 AM.

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

    Default

    Thanks liro, Twey.
    liro: Yes I have messenger but I don't use them, I just use online messeging services like meebo, gmail and e-messenger.net. I will try to execute your script and see if it works for me.

    liro: If you are talking about PHP scripts then I am not very good at it if javascript then I am not perfect at it.

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
  •