gpt4 book ai didi

mysql - 重音字符和 MySQL 搜索

转载 作者:行者123 更新时间:2023-11-29 03:43:30 24 4
gpt4 key购买 nike

我已经搜索了几个小时的问题,但没有找到适合我的答案,所以...我来了...

我是匈牙利人,我们在我们的语言中使用以下带重音的字符:áéíóöőúüüű(当然还有大写字母)我想在 php 中进行智能搜索,允许用户输入搜索词,它会在 MySQl 表或搜索字段中找到结果,无论是否有重音。所以……

我的 MySQL 表正在使用 utf8_hungarian_ci我可以进行 php 转换,以便用户输入“Bla”或“blá”,它将返回“bla”,我们正在搜索 mySQL 数据库。

但我的问题是……我的数据库可能有“bla fér”或“bláter”字段条目。但是,如果我们使用“bla”(来自 PHP)进行搜索,它只会返回“bla fer”。如何转换我正在搜索的字段以制作“bla fér”->“bla fer”和“bláter”->“blater”。所以本质上...

我想摆脱重音字符并将它们变成无重音字符。但当然,只是为了寻找。请帮忙!谢谢!

编辑:

<?php 
$search = $_GET["search"]; // May contain áéíóöőúüű
$accented= array("Ö","ö","Ü","ü","ű","Ó","ó","O","o","Ú","ú","Á","á","U","u","É","é","Í","í"," ","+","'","ő", "Ű", "Ő", "ä","Ä","ű","Ű","ő","Ő");
$nonaccented=array("O","o","U","u","u","O","o","O","o","U","u","A","a","U","u","E","e","I","i","_","_","_","o", "U", "O", "a","A","u","u","o","o");
$search = str_replace($accented,$nonaccented,$search);
$query = "SELECT id, name FROM people WHERE name LIKE '%$search%'"; // Database column 'name' may also contain áéíóöőúüű
?>

最佳答案

这是我的一些测试结果。您可以与您的比较:

CREATE TABLE `test` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(32) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8

表格内容:

mysql> select * from test;
+----+---------+
| id | name |
+----+---------+
| 1 | bla |
| 2 | blater |
| 3 | bláter |
| 4 | bhei |
+----+---------+
4 rows in set (0.00 sec)

搜索结果;

mysql> select * from test where name like '%bla%';
+----+---------+
| id | name |
+----+---------+
| 1 | bla |
| 2 | blater |
| 3 | bláter |
+----+---------+
3 rows in set (0.00 sec)

用重音搜索:

mysql> select * from test where name like '%blá%';;
+----+---------+
| id | name |
+----+---------+
| 3 | bláter |
+----+---------+
1 row in set (0.00 sec)

即使使用 COLLATE=utf8_hungarian_ci,我也得到相同的结果

关于mysql - 重音字符和 MySQL 搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10023776/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com