简体   繁体   中英

PHP MySQL search cyrillic characters

I would like to optimize my search sql query from cyrillic input.

If the user enters 'čšž' database return results with 'čšž' and 'csz'.

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

SQL or PHP should convert character to non cyrillic.

Any ideas?

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

This will do the trick using 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.

The server is using the wrong implementation of iconv. It has the glibc version instead of the required libiconv version.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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