【问题标题】:php string replace is not workingphp字符串替换不起作用
【发布时间】:2016-10-05 03:39:06
【问题描述】:

我想替换 sql 文件中的一些字符串。我写了一个 php 脚本,应该这样做。但是在我添加了循环通过那个大文件(1.2GB)的while之后,替换不再起作用了。

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

可能是什么问题?

【问题讨论】:

  • 好的,有什么解决办法?
  • 不是直接通过现有工具转换编码的选项吗?在 linux 命令行上?
  • 那会是什么?
  • 现在我混合了 latin1 和 utf8(不是我的失败是这样的),我希望得到正确的 utf8 和所有特殊字符
  • 有几种工具,您可以通过搜索引擎找到 - 其中之一:iconv - 另见stackoverflow.com/questions/11316986/…

标签: php


【解决方案1】:

解决方案是将 php 版本从 5.5 更改为 7

【讨论】:

    猜你喜欢
    • 2016-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-08
    • 2012-07-08
    • 2011-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多