4
I have a very strange problem with my PHP code.
All my charset are correct
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
I already use the code below to correct accent problems
header('Content-Type: text/html; charset=utf-8');
mysql_query("SET NAMES 'utf8'");
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_results=utf8');
When I send the form to Mysql it writes to the database correctly (latin1_swedish_ci database).
Everything appears perfectly on the screen.
The problem appears when I make a str_replace. When I take the URL variable by the $_GET method (which is with correct accent) and put it in the str_replace function, the function does not recognize the letter with accent and does not change.
Now, if I write in my code the same word ( <?php $str = "joão" ?>
for example ), hence the function recognizes the accent.
The problem also happens when the code compares a word with what is in the database. If he compares a letter with an accent that is in the code with a word that has an accent in the bank, he does not recognize it (then returns no result).
I’m using str_replace to search with REGEXP, so my code takes an accent word, swaps it for an equal word without accent (here I already have the error) and then creates a string with REGEXP to search.
Please help that I no longer know where to look!
Do not use two goal tags for the same purpose, the second is the improved version (HTML5) of the first. Only it already solves.
– Rafael Almeida
Friendly advice: check the files. Probably your files are encoded as iso8859-1, a good text editor like Sublime Text, Atom etc allows you to convert. About the database, if in the database you use iso8859-1 just remember to put the same ISO in the
mysql_query
. It is not "gambiarra" as the colleague said, it is an automatic PHP conversion system, which works very well if you know what you are doing. Just specify in PHP the same Sets pro database, table, fields etc. that you used in the database, and they convert everything perfectly.– Fernando Cordeiro