我已经搜索了几个小时的问题,但我找不到适合我的答案,所以...我来了......
我是匈牙利人,我们在我们的语言中使用以下重音字符:áéíóöőúüű(当然还有首都同行) 我想在php中进行智能搜索,允许用户输入搜索词,并在MySQl表或搜索字段中查找结果是否具有重音。所以......
我的MySQL表使用的是utf8_hungarian_ci 我可以进行php转换,以便用户输入'Bla'或'blá',它将返回'bla',我们正在搜索mySQL数据库。
但我的问题是......我的数据库可能有'blafér'或'bláter'字段条目。但是如果我们用'bla'(来自PHP)进行搜索,它只返回'bla fer'。如何转换我正在搜索的字段'blafé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 áéíóöőúüű
?>
答案 0 :(得分:0)
以下是我的测试结果。你可以与你比较:
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
答案 1 :(得分:0)
对我来说,将字符编码设置为utf8_general_ci(或使用&#34; _ci&#34;的其他内容)就可以了。 _ci表示SQL将匹配搜索,无论是大小写还是重音。