【问题标题】:How to convert Windows-1252 characters to values in php?如何将 Windows-1252 字符转换为 php 中的值?
【发布时间】:2016-05-16 00:27:46
【问题描述】:

我们有几个包含 Windows-1252 字符的数据库字段:

an example pain— if you’re

这些值映射到此列表中的所需值:

http://www.i18nqa.com/debug/utf8-debug.html

我尝试了 htmlentites、mb_detect_encoding、uft8_decode 等的各种排列,但还不能将这些值转换为:

痛苦的例子——如果你是

如何将这些字符转换为它们在 php 中列出的值?

【问题讨论】:

    标签: php utf-8 windows-1252


    【解决方案1】:

    您可以使用mb_convert_encoding

    $str = "an example pain— if you’re";
    $str = mb_convert_encoding($str, "Windows-1252", "UTF-8");
    echo $str;
    //an example pain— if you’re
    

    演示:
    http://ideone.com/NsIb5x

    【讨论】:

      【解决方案2】:

      天啊,解决这个问题的时间太长了,所以我想在这里发布我的答案,因为这个链接一直在搜索中出现。我的 MySQL DB 表使用 utf8mb4_unicode_520_ci 进行编码,并且一列有那些烦人的工作大引号。我试图读取数据库值并使用 json_encode 进行编码,但它会失败并且 json_encode 会返回空白,所以我使用了 utf8_encode。那不正确地转换了字符。我不得不使用 mb_convert_encoding 从 Windows-1252 转到 UTF-8,但后来 json_encode 也搞砸了。最后,这奏效了:

      $file = urlencode(mb_convert_encoding ($stringwithcurlyquotes, "UTF-8", 'Windows-1252'));
      

      由于我遇到了图片 URL 的问题,所以它工作得很好,不需要我在另一边对其进行解码。

      【讨论】:

        猜你喜欢
        • 2019-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多