SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: Edit a file

  1. #1
    SitePoint Member rupam_jaiswal's Avatar
    Join Date
    Jun 2006
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Edit a file

    Hi,
    Please excuse if similar query has been posted earlier.

    I have text file in which I have to search a word and replace it with a new word.

    First I do is to get the file contents with fopen,then read the contents , maintain a string $newcontent that holds the contents of file ,the moment i get my word,replace it with new word in the string $newcontent
    After that I write the whole string $newcontent to file again.
    What I want is to just while scanning the file,the moment I find my word,replace it with new word there only instead of rewriting the whole file again.
    Regards

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,510
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    As far as I know, you can't. But... I don't know everything
    So let's see what others have to say.

  3. #3
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe:
    PHP Code:
    $fname "test.txt";
    $lines file($fname);
    $out "";

    $pattern '/ddd+/';
    $replacement 'replace';

    foreach (
    $lines as $line)
    {
        
    $out .= preg_replace($pattern$replacement$line);
    }

    $f fopen($fname"r+");
    fwrite($f$out);
    fclose($f); 
    test.txt
    Code:
    aaa bbb ccc ddd
    eee
    ddd
    my mobile portal
    ghiris.ro

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,510
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    You still read, change and then rewrite the whole file though.

  5. #5
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,398
    Mentioned
    65 Post(s)
    Tagged
    1 Thread(s)
    If the replacement string is the same length (in bytes) as the original, then you can just over-write the bytes. Otherwise, the best you can get away with is writing from the insertion point to the end of the file. The easiest would be just writing the entire file.
    Salathe
    Software Developer and PHP Manual Author.

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You could, read the file a character at a time, if you find your word, record the position, place your new word in at that position and start the process again from the last recorded position plus the length of the replacement word.

    *phew*
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


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
  •