【问题标题】:How to convert String with “ (ISO-8859-1) characters to normal (UTF-8)characters?如何将带有“(ISO-8859-1)字符的字符串转换为普通(UTF-8)字符?
【发布时间】:2018-06-11 05:33:13
【问题描述】:
<li>Jain R.K. and Iyengar S.R.K., “Advanced Engineering Mathematicsâ€, Narosa Publications,</li>

我在数据库中有很多原始的 html 字符串。所有的文字都有这些奇怪的字符。我如何转换为普通文本以将其保存回数据库中。

$final = '<li>Jain R.K. and Iyengar S.R.K., “Advanced Engineering Mathematicsâ€, Narosa Publications,</li>';
$final = utf8_encode($final);

$final = htmlspecialchars_decode($final);

$final = html_entity_decode($final, ENT_QUOTES, "UTF-8");

$final = utf8_decode($final);

echo $final;

我尝试了上面的代码,它在网络浏览器中正确显示,但仍然在数据库中保存相同的奇怪字符。

数据库的字符集是utf-8

【问题讨论】:

    标签: php mysql utf-8 character-encoding iso-8859-1


    【解决方案1】:

    “ 的“Mojibake”。您可以尝试避免使用非 ascii 引号,但这只会延迟重新陷入麻烦。

    您需要在表和连接中使用utf8mb4。请参阅 this 了解 Mojibake 的可能原因。

    【讨论】:

      【解决方案2】:
      $final = '<li>Jain R.K. and Iyengar S.R.K., “Advanced Engineering Mathematicsâ€, Narosa Publications,</li>';
      
      $final = str_replace("Â", "", $final);
      $final = str_replace("’", "'", $final);
      $final = str_replace("“", '"', $final);
      $final = str_replace('–', '-', $final);
      $final = str_replace('â€', '"', $final);
      

      对于过去的数据,我用 UTF-8 字符替换了奇怪的字符。

      对于未来的数据,我在 php、html 和数据库连接中将字符集设置为 utf8。

      【讨论】:

        【解决方案3】:

        使用ftfy工具修复文本更安全https://ftfy.readthedocs.io/en/latest/

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-29
        • 2014-07-04
        • 2020-01-25
        • 2012-01-05
        • 1970-01-01
        • 2016-07-29
        • 2011-12-15
        相关资源
        最近更新 更多