I'm storing a varchar in a utf8 MySQL table and using utf8_general_ci collation. I have a unique index on the varchar. I'd like to do a string comparison in PHP that is equivalent to what MySQL will do on the index.

我将varchar存储在utf8 MySQL表中并使用utf8_general_ci排序规则。我在varchar上有一个唯一的索引。我想在PHP中进行字符串比较,这与MySQL在索引上的操作相同。

A specific example is that I'd like to be able to detect that 'a' is considered equivalent to 'À' in PHP before this happens:

一个具体的例子是我希望能够在发生这种情况之前检测到'a'在PHP中被认为等同于'À':

mysql> insert UniTest (str) values ('a');                                   
Query OK, 1 row affected (0.00 sec)

mysql> insert UniTest (str) values ('À');                                   
ERROR 1062 (23000): Duplicate entry 'À' for key 1

5 个解决方案

#1


9

The collation has nothing to do with the storage. You need to set the charset to determine the storage encoding. The collation governs how comparison and sorting should happen. The collation must be charset aware, but otherwise it has nothing to do with the charset.

整理与存储无关。您需要设置charset以确定存储编码。整理控制着如何进行比较和排序。整理必须是charset意识,否则它与charset无关。

To answer your question, you can use iconv to translitter the text, and then compare it. For example:

要回答您的问题,您可以使用iconv转换文本,然后进行比较。例如:

function compare($s1, $s2) {
  return strcmp(
    iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $s1),
    iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $s2));
}

This is basically what MySql will do for you, although it's probably faster and it may have a slightly different collation-table than ISO-8859-1//TRANSLIT. Not entirely sure about that.

这基本上是MySql将为您做的,虽然它可能更快,并且它可能与ISO-8859-1 // TRANSLIT略有不同的校对表。对此并不完全确定。

Would probably be easier to use the database though, as others have already suggested.

可能更容易使用数据库,正如其他人已经建议的那样。

更多相关文章

  1. 为什么MySQL查询优化器会选择聚集主索引上的二级索引?
  2. 如何用C++向MYSQL插入UNICODE字符串?
  3. Mysql索引基础原理
  4. 由于底层异常,无法加载连接类:'java.lang.NumberFormatException:对
  5. 获取特定行的索引
  6. MySQL判断索引存在并删除索引的存储过程
  7. 0926MySQL中ICP索引下推
  8. mysql 操作索引FORCE INDEX
  9. InnoDB辅助索引页面的物理结构是什么样子的

随机推荐

  1. Android Contacts的使用(二)
  2. Android超炫日期日历控件:TimesSquare
  3. Android不同层次开启硬件加速的方式
  4. Android官方ORM Room介绍
  5. android集合SSH搭建服务器客户端请求
  6. android检测网络状态
  7. Android获取屏幕宽度的4种方法
  8. Android UI属性大解
  9. 国内目前最全面的介绍——Android中的Bro
  10. Hook android系统调用研究(一)