Pergunta

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?

Nenhuma solução correta

Outras dicas

You should use UTF8_unicode_ci collation for your column (or table or whole database) and then database should handle all that you want.

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top