1

I want to replace some strings in a sql file. I've written a php script that should do that. But after I added the while for looping trough that big file (1.2GB) the replacing is not working anymore.

$reading = fopen('m.sql', 'r');
$writing = fopen('m.tmp', 'w');

$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";

What could be the problem?

Felix
  • 5,452
  • 12
  • 68
  • 163
  • okay and what could be a solution? – Felix Jun 05 '16 at 12:50
  • Isn't it an option to convert the encoding directly by an existing tool e.g. on the linux command line? – Pinke Helga Jun 05 '16 at 12:51
  • and what could that be? – Felix Jun 05 '16 at 12:51
  • now I have mixed latin1 and utf8 (not my fail got it like this) at the end I want to have proper utf8 with all special chars – Felix Jun 05 '16 at 12:52
  • There are several tools, you could find by a search engine - one of them: iconv - see also http://stackoverflow.com/questions/11316986/how-to-convert-iso8859-15-to-utf8 – Pinke Helga Jun 05 '16 at 12:53
  • Yes I tried iconv but the result isn't fine. When I have used this `$dateiinhalt = file_get_contents("./m.sql");` it worked fine. Problem is that the file is to big for file_get_contents – Felix Jun 05 '16 at 12:54
  • `fgets` read 4096 bytes, it can cut part of symbol, one symbol != one byte. – jekaby Jun 05 '16 at 12:56
  • okay can you tell me how I can fix that? – Felix Jun 05 '16 at 12:57
  • why dos it work with `file_get_contents` is there a way to use `file_get_contents` in this large file? – Felix Jun 05 '16 at 13:01
  • It should work with reading the file line by line as well. Otherwise I only guess there might be a BOM at the start of file.(?) However, afaik PHP does not care about a BOM. Are you only dealing with *one* big file? – Pinke Helga Jun 05 '16 at 13:04
  • how can I check if there is a bom when I can't open the file? – Felix Jun 05 '16 at 13:05
  • how could it look like in recode? tried `recode ../utf8 m.sql` not working ... – Felix Jun 05 '16 at 13:08
  • Have you tried `recode latin1..utf8 m.sql` – Pinke Helga Jun 05 '16 at 13:14
  • If none of the above solves you problem, you could take a look at [mb_* functions](php.net/manual/en/ref.mbstring.php) – Pinke Helga Jun 05 '16 at 13:22
  • recode has broken the file. its emty now which mbstring function? – Felix Jun 05 '16 at 13:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/113845/discussion-between-quasimodos-clone-and-felix). – Pinke Helga Jun 05 '16 at 13:34

1 Answers1

0

Solution was to change php version from 5.5 to 7

Felix
  • 5,452
  • 12
  • 68
  • 163