dongtuwu8548 2012-07-15 17:12
浏览 90
已采纳

如何在PHP中安全地将UTF-8与ISO 8859-1(latin1)进行比较?

This might be a stupid question, but nothing seems to be working for me:

I'm having to compare values between 2 columns on 2 different databases (which I don't have access to change the values). The encoding in db1 is UTF-8. The encoding in db2 is latin1.

So, for example, these are the 2 values I'm comparing and should be the same in the comparison:

**db1_value** = 'Maranhão'
**db2_value** = 'Maranhão';

They display exactly the same way using utf_encode, displaying is not the issue. I'd like to compare the variable db1_value to the field db2_value in the db, so I'm using something very simple like this:

$query = "SELECT **db2_value** FROM db2 WHERE db2_field LIKE '" . **$db1_value** . "'";

How do I convert 'Maranhão' into '**Maranhão**' before comparing?

I've tried several methods, iconv, utf8_encode, and a few others, but they make no difference to the variable. I'm just wondering if I'm taking the right approach to do this.

Appreciate any constructive comments on this.

Thanks a lot,

  • 写回答

1条回答 默认 最新

  • drwxg62286 2012-07-15 17:24
    关注

    You need to convert not from UTF-8 but from HTML-ENTITIES into actual value Luckily mbstring extension has such conversion available:

    $latin1 = mb_convert_encoding($db1_value, "ISO-8859-1", "HTML-ENTITIES");
    

    Here we specify the HTML-ENTITIES as the FROM charset

    Then you can compare $latin1 to your $db2_value.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)