Bring sql record even not having the necessary accent [duplicate]

3

I need to bring a record called Vidraçaria into a search, how can I bring this record by just writing Vidracaria in the search?

  

Or bring water by looking only agua

The search works normal when the word is spelled correctly, with or without an accent.

I'm using like () to fetch the information in the mysql records

    
asked by anonymous 26.11.2018 / 20:37

2 answers

4

Use a function to remove accents from the searched word and from the bank. This way you will find the word with or without accents:

function tiraacentos($i){
   return preg_replace(array("/(á|à|ã|â|ä|Á|À|Ã|Â|Ä)/","/(é|è|ê|ë|É|È|Ê|Ë)/","/(í|ì|î|ï|Í|Ì|Î|Ï)/","/(ó|ò|õ|ô|ö|Ó|Ò|Õ|Ô|Ö)/","/(ú|ù|û|ü|Ú|Ù|Û|Ü)/","/(ñ|Ñ)/","/(ç|Ç)/","/(ý|ÿ|Ý)/"),explode(" ","a e i o u n c y"),$i);
}

In the query you use the function in the DB table name and the searched term:

$palavra = $_POST['palavra'];
"SELECT * FROM tabela WHERE ".tiraacentos(NOME DA TABELA)." LIKE '%".tiraacentos($palavra)."%'"
    
26.11.2018 / 21:59
1

You can use a different collation at run time to remove accents:

...
WHERE nome like '%vidracaria%' collate utf8_general_ci

Or for the case of the error pointed out:

...
WHERE nome like '%vidracaria%' collate utf8mb4_general_ci
    
26.11.2018 / 20:55