3

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

我是匈牙利人,我们在我们的语言中使用以下重音字符:áéíóöőúüű(当然还有大写字母)我想在 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 áéíóöőúüű
?>
4

2 回答 2

1

以下是我的一些测试结果。你可以和你的比较:

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

于 2012-04-05T07:34:29.343 回答
0

对我来说,将字符编码设置为 utf8_general_ci(或其他带有“_ci”的东西)就可以了。_ci 表示 SQL 将匹配搜索而不考虑大小写或重音。

于 2018-06-02T08:11:01.397 回答