I tried to replace single quotes in a large XML file(110MB) with this code but an error occured. I need a code that can handle atleast 3GB XML file.
Error Message:
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20449728 bytes) in C:\xampp\htdocs\replace.php on line 10
<?php replace_file('electronics.xml', "'", "'"); function replace_file($path, $string, $replace) { $file = fopen($path, 'a+'); while (feof($file) === false) { $str=file_get_contents($path); $str=str_replace($string, $replace, fgets($file)); } fclose($file); } echo "replace done"; ?>
file_get_contents, You are reading the whole file for each line iteration. Usefreador something similar