Problems with PHP encoding in UTF-8

Asked

Viewed 1,461 times

3

None of the solutions suggested in various forums, websites and the like worked. I performed several tests and none of them worked and so I came here.

The website where the error occurs is this: http://simuladoconcursosbrasil.com.br/

It was not developed by me, a client of mine requested that I fix this problem for him.

All special characters are always replaced by à . I tried to change the encoding to ISO and the error remained, I tried to change the coding in the database, force UTF-8 in the header, put the javascript to interpret as UTF-8 and none of it worked.

The connection in the database is as follows:

<?php
$bd_host = "localhost";
$bd_user = "simulado_bd"; // Usuário do Banco de Dados
$bd_pass = "********"; // Senha do Bando de Dados
$bd_bd = "simulado_bd"; // Nome do Banco de Dados
$conectar = mysql_connect($bd_host, $bd_user, $bd_pass) or die (mysql_error());
mysql_select_db($bd_bd, $conectar);
mysql_query('SET CHARACTER SET utf8');
  • 1

    What was suggested? what is the file Find? of the base?

  • Have you tried using the HTML5 <meta charset="UTF-8"> tag? It doesn’t cost to try :)

  • How is your connection class to the database?

  • 1
  • 2
  • UTF-8 encoding files, no BOM, UTF-8 general ci database

  • Sanction, as I mentioned above, was not programmed by me, I have not used mysql_ functions for a long time.

  • So far none of the tips solved. The data in the database are in UTF-8, but are not read as they should.

  • @Raphaelcordeiro, try adding the charset to the HTTP header with header('Content-Type: text/html; charset=utf-8');

  • @Sanction I tried to use your suggestion and the error remained.

  • 2

    Looking at the source of the page, it seems to me that several attempts have been made to tidy up the data, including "search and replace" manually. It is probably the case to review the database directly, and then fix PHP. It seems to me to have entities mixed with wrong characters, and even double Encounter.

  • Raphael, if it is related only to contents coming from the base, you need to see how it was recorded at the base, is that it is not worth doing an inclusion test of any accented text and then read it using utf8_encode / utf8_decode?

  • And how would I do that, @Thyagothysoft ? Bacco, I tried to make a substitution to fix yes, using the codes referring to accented characters, but it didn’t work, as you can notice.

  • 1

    @Raphaelcordeiro the first thing would be to tidy up the database then, even manually. If you keep trying to tidy up by PHP, you may have apparently solved it, but you end up creating or hiding a bigger problem. Once you are sure that DB is correct, you can try to solve PHP. PS: Remember first of all, restore the DB backup from before attempts, so as not to complicate too much.

  • One thing seems certain, are two encoding errors, the menu presents a type of error of symbols characters, already the questions and answers that I believe are DB seems to be another encoding error.

  • The ones I need to solve are just the ones pulled from the database, the other coding problem I can solve. I will restore the backup of the script database, the way it came (and worked on another server).

  • If you can make a test.... In the inclusion form for the BD, send the fields like this: htmlspecialchars($_POST['blablabla'], NULL, 'UTF-8') can you see how you are? At least you will be sure of a new content included in utf-8.

  • Read the dice coming from the comic so: utf8_encode($resultado) these tests are for tracking, we will find the problem xD

Show 13 more comments

3 answers

6


I’ve had the same problem. I managed to solve it this way:

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');

Put all lines after your connection, it will probably work!

  • Didn’t work :/

  • Do you have access to the php.ini file on the server? If so, check that the default charset is defined as UTF-8

3

Make sure the file is converted to UTF-8 without BOM / UTF-8 sem BOM. Check that the Mysql database is as UTF_8.

If both are correct, do the following with your pages in php

header('Content-Type: text/html; charset=utf-8');<meta charset="utf-8" /> 

and in html

<meta charset="utf-8" /> 
  • It didn’t work anyway. Like, I realized that when I edit out the questions and answers, it stops being "A" instead of the accented characters and starts to appear "?"

  • The à that appears is different, because it is a special character generated by the attempt to convert the char... You have checked if your connection file, the file that works the data and the database are as utf8 without BOM? And check your browser also if it is with the correct encoding

3

Maybe help someone, I was in trouble at AJAX.

I was using in php:

*header('Content-Type: text/html; charset=utf-8');* 

I switched to:

*header('Content-type: text/html; charset=iso-8859-1');*
  • this would be correct header('Content-Type: text/html; charset=utf-8');, you must be using file with GOOD

  • 1

    I thought I was @Willbb, but no, I changed the format and now it’s working normal.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.