2

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', "'", "&#39;"); 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"; ?> 
2
  • Drop the file_get_contents, You are reading the whole file for each line iteration. Use fread or something similar Commented May 11, 2015 at 10:10
  • 110MB for a single XML file is quite much, but as long as the code is well written you won't be using more than 220-250 Mb of memory. That said, I would rather recommend you to load the entire file, replace the string and savfe it back somewhere else. Before saving it, unset the original needle to free some memory. In this way you are using file_get_content for each line of iteration, which is actually why you are finishing your memory. Also, I would recommend you to increase the maximum memory from your php.ini. Commented May 11, 2015 at 10:16

3 Answers 3

2

Reading a large file into php is not recommended. Call a command line that is appropriate, like sed

Reference: http://www.grymoire.com/Unix/Sed.html

Sign up to request clarification or add additional context in comments.

Comments

1

Simplify:

$str = str_replace( "'","&#39;",file_get_contents('electronics.xml')); 

This is just very wrong:

Opening XML

 $file = fopen($path, 'a+'); 

While Loop for no reason, fgets reads to end of file, so loop completes on first iteration.

 while (feof($file) === false) { 

reading in entire contents of same file file again, for no purpose

 $str=file_get_contents($path); 

Reading in entire file, no length specified, so reading to EOF

 $str=str_replace($string, $replace, fgets($file)); } fclose($file); 

Nothing accomplished.

Comments

1
 //// //PHP 5.3 + Class find and replace string in files // //by Bruce Afruz // //2013 // //example usage for single file: // //$new = new fileReplacement('./'); //$new->setExt("check.php"); //$new->changeContents("hello", "goodbye"); // //example usage for multiple files: // //$new = new fileReplacement('./test'); //$new->setExt("*.html"); //$new->changeContents("hello", "goodbye"); // //to change directory: // //$new = new fileReplacement('./test'); //$new->setDir("./test2"); //$new->setExt("*.html"); //$new->changeContents("hello", "goodbye"); //// class fileReplacement { private $ext , $dir ; public function getDir() { return $this->dir; } public function setDir($dir) { $this->dir = $dir; } public function getExt() { return $this->ext; } public function setExt($ext) { $this->ext = $ext; } function __construct($dir) { $this->dir = $dir; } public function rglob($pattern = '*', $flags = 0, $path = '') { chdir($this->getDir()); $paths = glob($path . '*', GLOB_MARK | GLOB_ONLYDIR | GLOB_NOSORT); $files = glob($path . $pattern, $flags); foreach ($paths as $path) { $files = array_merge($files, $this->rglob($pattern, $flags, $path)); } return $files; } public function changeContents($replace , $sentence , $flags = 0, $path = '') { $all = $this->rglob($this->getExt() , $flags, $path); foreach ($all as $file) { $filename = $file; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); fclose($handle); $contents = str_replace($replace , $sentence, $contents); if (is_writable($filename)) { if (!$handle = fopen($filename, 'w+')) { echo "Cannot open file ($filename) "; exit; } // Write $contents to our opened file. if (fwrite($handle, $contents) === FALSE) { echo "Cannot write to file ($filename) "; exit; } echo "Success, wrote content to file ($filename) "; fclose($handle); } else { echo "The file $filename is not writable "; } } }} 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.