normalizing accented characters in MySQL queries

后端 未结 5 1859
-上瘾入骨i
-上瘾入骨i 2020-12-03 17:33

I\'d like to be able to do queries that normalize accented characters, so that for example:

é, è, and ê

are all treated as \'e\', in querie

5条回答
  •  一生所求
    2020-12-03 17:58

    I'd suggest that you save the normalized versions to your table in addition with the real username. Changing the encoding on the fly can be expensive, and you have to do the conversion again for every row on every search.

    If you're using PHP, you can use iconv() to handle the conversion:

    $username = 'rené';
    $normalized = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
    

    Then you'd just save both versions and use the normalized version for searching and normal username for display. Comparing and selecting will be alot faster from the normalized column, provided that you normalize the search string also:

    $search = mysql_real_escape_string(iconv('UTF-8', 'ASCII//TRANSLIT', $_GET['search']));
    mysql_query("SELECT * FROM User WHERE normalized LIKE '%".$search."%'");
    

    Of course this method might not be viable if you have several columns that need normalizations, but in your specific case this might work allright.

提交回复
热议问题