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?
views:
577answers:
5Get 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.
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');
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);
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");
@Alix : hello, $file variable is not defined in your function, how could it work? you have to extract file from path?