views:

577

answers:

5

I am trying to do a string replace for entire file in PHP. My file is over 100MB so I have to go line by line and can not use file_get_contents(). Is there a good solution to this?

+1  A: 

Get it a few lines at a time, dump the variable, get the next few lines.

$fh = fopen("bigfile.txt", "flags");
$num = 0;
$length = 300;
$filesize = filesize("bigfile.txt");

while($num < $filesize)
{
     $contents = fread($fh, $length);
     // .. do stuff ...
     $num = $num+$length;
     fseek($fh, $num);
}

fclose($fh);

You are going to want to make sure that is correct (haven't tested). See the library on PHP Documentation.

The tricky part is going to be writing back to the file. The first idea that pops into my mind is do the string replace, write the new content to another file, and then at the end, delete the old file and replace it with the new one.

Chacha102
yea that's right... a poor man's streaming. That effectively how it would work if you did it on the command line: cat file | sed 's/replace/something/g' > output.file
mlathe
I would run a php script that called the command line function -- then printed out the output file :)
Michael
This method has a problem if the string you're looking to replace is longer than a single character. It's quite possible that the string could span multiple chunks of data, causing you to skip the replacement.
bish
A: 

Here you go:

function replace_file($path, $string, $replace)
{
    set_time_limit(0);

    if (is_file($file) === true)
    {
        $file = fopen($path, 'r');
        $temp = tempnam('./', 'tmp');

        if (is_resource($file) === true)
        {
            while (feof($file) === false)
            {
                file_put_contents($temp, str_replace($string, $replace, fgets($file)), FILE_APPEND);
            }

            fclose($file);
        }

        unlink($path);
    }

    return rename($temp, $path);
}

Call it like this:

replace_file('/path/to/fruits.txt', 'apples', 'oranges');
Alix Axel
I'm just guessing you had this function lying around?
Chacha102
@Chacha102: No, I just coded it. Why?
Alix Axel
@Alix It was kind of a joke. I don't know why it would have been funny. I'm going to give it to our JDT (Joke Development Team) and get back to you in 6 to 8 weeks.
Chacha102
@Chacha1024: lol =P
Alix Axel
A: 

something like this?

$infile="file";
$outfile="temp";
$f = fopen($infile,"r");
$o = fopen($outfile,"a");
$pattern="pattern";
$replace="replace";
if($f){
     while( !feof($f) ){
        $line = fgets($f,4096);
        if ( strpos($pattern,"$line") !==FALSE ){
            $line=str_replace($pattern,$replace,$line);
        }
        fwrite($o,$line);
     }
}
fclose($f);
fclose($o);
rename($outfile,$infile);
ghostdog74
+5  A: 

If you aren't required to use PHP, I would highly recommend performing stuff like this from the command line. It's byfar the best tool for the job, and much easier to use.

In any case, the sed command is what you are looking for:

sed s/search/replace oldfilename > newfilename

If you need case-insensitivity:

sed -i s/search/replace oldfilename > newfilename

If you need this to perform dynamically within PHP, you can use passthru():

$output = passthru("sed s/$search/$replace $oldfilename > $newfilename");
Dominic Barnes
why `passthru()` and not `exec()`?
Brian
Either would work, but using passthru, you can get the entire output. exec only returns the last line of the output.
Dominic Barnes
A: 

@Alix : hello, $file variable is not defined in your function, how could it work? you have to extract file from path?

dave