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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器