Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2004
    Posts
    30

    Unanswered: how to edit file's contents with php(insert line in between)

    i have a file with the contents of :
    apple1
    orange2
    lime3
    strawberry4
    pineapple5
    mango6
    i would like to insert one newline "newline" in between "lime3" and "strawberry4"

    first i wrote my code like this:
    Code:
    <?php
    
    $fp = fopen("data/file.txt", "r+");
    
    while($buf = fgets($fp)){
        if(preg_match("/strawberry/", $buf)){
            fputs($fp, "newline\n");
        }
    }
    ?>
    but it ended up like this:
    apple1
    orange2
    lime3
    strawberry4
    newline
    e5
    mango6
    so i changed my code to the following (omitting \n at the end of "newline")
    Code:
    <?php
    
    $fp = fopen("data/file.txt", "r+");
    
    while($buf = fgets($fp)){
        if(preg_match("/strawberry/", $buf)){
            fputs($fp, "newline");
        }
    }
    ?>
    but i got the result of
    apple1
    orange2
    lime3
    strawberry4
    newlinele5
    mango6
    i cant seem to have a line inserted in between..
    it replaces the line after it..

    i would appreciate any help
    thanks

  2. #2
    Join Date
    Feb 2004
    Posts
    533
    You are opening the file for read + edit and traversing the file with fgets() this is not creating any new space for the text you are writing. It just writes the text at the file pointer location overwriting any existing text. You need to read in all of the text from the file then open the file and re-write all the text back in including the new values you want added.

    This is an example I came up with. I found there was a problem with the file changing before the buffer was completed reading the file which caused two entries to be added for the new value. To prevent this I created a temp file to write to, then copied it back to the original name. I tested it on windows maybe this wouldn't occur on unix or Linux? but this should work on either.
    PHP Code:
    <?

    $key 
    'strawberry4';
    $newline 'blackberry';

    //copy file to prevent double entry
    $file "file.txt";
    $newfile "filetemp.txt";
    copy($file$newfile) or exit("failed to copy $file");

    //load file into $lines array
     
    $fc fopen ($file"r");
     while (!
    feof ($fc)) 
     {
        
    $buffer fgets($fc4096);
        
    $lines[] = $buffer;
     }

    fclose ($fc);

    //open same file and use "w" to clear file 
    $f=fopen($newfile,"w") or die("couldn't open $file");

    /* uncomment to debug
    print_r($lines);
    print "<br>\n";
    */

    //loop through array using foreach
    foreach($lines as $line)
    {
           
    fwrite($f,$line); //place $line back in file    
        
    if (strstr($line,$key)){ //look for $key in each line
        
    fwrite($f,$newline."\n");
        } 
    //place $line back in file 
    }
    fclose($f);

    copy($newfile$file) or exit("failed to copy $newfile");
    ?>
    ~

    Bill

  3. #3
    Join Date
    Jul 2004
    Posts
    30
    savbill,

    thanks!!

  4. #4
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    Although that solution works, it might make more sense to copy the file to a temporary location, then read through that (temporary) file, doinf what ever procressing you need then overwrtiting the original file line by line.

    The reason?
    you are not consuming resources within the app, there is no risk of blowing the memory limit, or finding that you are unable to process large files
    its possibly doing less work on the server so your script may be using less resources.

    its arguable that you should create the new file in the temporary location, assuming all goes well then copy the temporary file over the original file as a fail safe. ie if the script fails the original file is left as is, bu as ever its up to you
    I'd rather be riding on the Tiger 800 or the Norton

Posting Permissions

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