简体   繁体   English

PHP MySQL搜索西里尔字符

[英]PHP MySQL search cyrillic characters

I would like to optimize my search sql query from cyrillic input. 我想从西里尔文输入优化我的搜索SQL查询。

If the user enters 'čšž' database return results with 'čšž' and 'csz'. 如果用户输入“čšž”数据库,则返回结果为“čšž”和“ csz”。

$result = mysqli_query($con,"SELECT * FROM Persons WHERE name = ?");

SQL or PHP should convert character to non cyrillic. SQL或PHP应该将字符转换为非西里尔字母。

Any ideas? 有任何想法吗?

您应该对列(或表或整个数据库)使用UTF8_unicode_ci排序规则,然后数据库应处理所需的所有操作。

This will do the trick using iconv : 这将使用iconv来解决问题

// your input
$input = "Fóø Bår";

setlocale(LC_ALL, "en_US.utf8");

// this will remove accents and output standard ascii characters
$output = iconv("utf-8", "ascii//TRANSLIT", $input);

// do whatever you want with your output, for example use it in your MySQL query
// or just "echo" it, for demonstration
echo $output;

Note that this may not work as expected when a wrong version of iconv is installed on the server. 请注意,当服务器上安装了错误版本的iconv时,这可能无法正常工作

The server is using the wrong implementation of iconv. 服务器正在使用错误的iconv实现。 It has the glibc version instead of the required libiconv version. 它具有glibc版本,而不是必需的libiconv版本。

In that case, you may use this ugly hack that will work for most cases : 在这种情况下,您可以使用这种适用于大多数情况的丑陋技巧:

function stripAccents($stripAccents){
  return strtr($stripAccents,'àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ','aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY');
}

Taken from this answer . 取自这个答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM