I've got a 1.5gig big file where I want to do some replacements.
But my apache runs out of memory. Is there a possibility to this job line by line?
<?php
$myfile = fopen("./m.sql", "r") or die("Unable to open file!");
$dateiinhalt = file_get_contents("./m.sql");
$search = array("ä",
"Ä",
"ö",
"Ö",
"ü",
"Ü",
"€",
"§",
"ß",
"latin1_general_ci",
"CHARSET=latin1",
"‚",
"„",
"‘",
"’",
"“",
"â€",
"©",
"®");
$replace = array("ä",
"Ä",
"ö",
"Ö",
"ü",
"Ü",
"€",
"§",
"ß",
"utf8_general_ci",
"CHARSET=utf8",
"‚",
"„",
"‘",
"’",
"“",
"”",
"©",
"®");
$content = str_replace($search, $replace, $dateiinhalt);
file_put_contents("./m.sql", $content);
echo "done";
thanks at all
Updated Code:
$reading = fopen('m.sql', 'r');
$writing = fopen('m.tmp', 'w');
$replaced = false;
$search = array("ä",
"Ä",
"ö",
"Ö",
"ü",
"Ü",
"€",
"§",
"ß",
"latin1_general_ci",
"CHARSET=latin1",
"‚",
"„",
"‘",
"’",
"“",
"â€",
"©",
"®");
$replace = array("ä",
"Ä",
"ö",
"Ö",
"ü",
"Ü",
"€",
"§",
"ß",
"utf8_general_ci",
"CHARSET=utf8",
"‚",
"„",
"‘",
"’",
"“",
"”",
"©",
"®");
if ($reading) {
// read one line or 4096 bytes, whichever comes first
while (($dateiinhalt = fgets($reading, 4096)) !== false) {
// replace in that and write to output file
fwrite($writing, str_replace($search, $replace, $dateiinhalt));
}
if (!feof($reading)) { // fgets() failed, but not at end of file
echo "Error: unexpected fgets() fail\n";
}
fclose($reading);
fclose($writing);
}
echo "done";