PHP accent problem, MySQL

2

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 bank with an accent correctly (database in latin1_swedish_ci).

Everything appears perfectly on the screen.

The problem appears when I make a str_replace. When I get the URL variable using the $ _GET method (which has the correct accent) and put it in the str_replace function, the function does not recognize the letter with an accent and does not make the change. Now, if I write the same word in my code ( <?php $str = "joão" ?> for example), then the function recognizes the accent.

The problem also happens when the code compares a word to what is in the database. If it compares a letter with an accent that is in the code with a word that has an accent in the database, it does not recognize (so it does not return any results).

I'm using str_replace to do searches with REGEXP, so my code takes a word with an accent, swap for an equal word without an accent (here I already have the error) and then create a string with REGEXP to search.

Please help me no longer know where to look!

    
asked by anonymous 20.04.2015 / 23:08

1 answer

2

Database latin1_swedish_ci should not be used with utf8 , otherwise you will stay in the game to solve the problem.

If you use latin1 , then use <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">

This problem occurs because utf8's accented characters are totally different from latin1, so the bank is two different things.

If you are going to use pages in utf-8 , then use the bank in utf-8 , I recommend reading this:

21.04.2015 / 03:35