与带有重音符号、变音符号等的单词匹配 mysql/php
我有一个网站,我刚刚为其编写了一个很棒的搜索功能。我刚刚意识到我的数据库中有一些带有重音符号的单词。因此,当有人输入要搜索的单词(当然没有重音符号)时,他们将找不到他们要查找的内容。
目前大多数搜索功能已经解决了这个问题;他们是怎么做到的?其中一定有什么巧妙的技巧。我的大多数查询都使用 mysql 的 MATCH 功能,但其中之一使用 LIKE。
I've got a website for which I just wrote a great search function. I just realized that I have some words in my db with accent marks. So when somebody types in the word to search for, without the accent mark of course, they don't find what they are looking for.
most search functions have solved this problem by now; how do they do it? There must be some clever trick to it. Most of my queries use mysql's MATCH feature but one of them uses LIKE.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要设置特定的 排序规则您的模式,以便 MySQL 知道哪些字符应该是等效的。带变音符号的字母是否被视为与不带变音符号的字母或其他字符序列相同取决于语言,但请尝试 utf8_general_ci 作为忽略重音的起点(假设您使用的是 UTF-8 字符集,您可以使用 utf8_general_ci 作为忽略重音符号的起点)应该是真的)。
尝试对所有字符串使用相同的排序规则,以便可以连接和比较它们,而无需进行昂贵的、破坏索引的转换。
You need to set a particular collation on your schema so that MySQL knows which characters are supposed to be equivalent. Whether a letter with a diacritical mark is considered the same as one without or some other sequence of characters is language-dependent, but try utf8_general_ci as a starting point for ignoring accents (assuming you're using a UTF-8 character set, which you should be really).
Try to use the same collation on all your strings, so that they can be joined and compared without expensive, index-breaking conversions.