How to convert UTF-8 characters in PHP

10

In my web application, I'm using a MySQL database that has the EditableContent table. This table has the content field, which is of type text and has the input format utf8-bin .

In the database the characters appear normally, but when searching for them and displaying them via echo method, accented characters are replaced with . When using the uf8_decode($meu_texto) method, the same characters are replaced with ? .

In one row of the table, the characters are shown normally, and in another, only the initial part.

Obs. I'm using the meta tag:

'<meta http-equiv="Content-Type" content="text/html; charset=utf-8">'

And the content was copied to the NotePad ++ database, the formatting of the file being as Codificação em UTF-8(Sem BOM)

    
asked by anonymous 19.08.2014 / 02:19

6 answers

14

Let's see ..

What fellow @Marta and @Marcelo said is true. However, sometimes it may be that the web server used is not configured to pass UTF-8 - even if it uses the <meta ../> or the header() function of PHP. If it's a shared VM host, even if it's paid (cheap / free host is guaranteed to be), you'll have to ask the server support to change it for you - I've had this problem. Try this if nothing below works.

1 - Through the tag in HTML

In HTML5 'default', you can use

<meta charset="utf-8" />

In previous standards, you can use

<meta http-equiv="content-type" content="text/html; charset=utf-8" />

2 - Through PHP

You can configure the pure page in PHP and call the function

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

3 - Check the configuration of the bank tables

It may be that charset or collade are not UTF-8. Worth checking out

4 - Pure PHP page without header()

If the page is in pure PHP or a page Ajax call in pure PHP and there is no header() with the charset set in both cases, it will not be set to UTF-8. This is probably your problem. But it will not do any good if HTML is not in UTF-8 too, of course.

Note: It is valid for redirects as well.

    
19.08.2014 / 07:47
8

Some things can help, such as

  • save the file even with the correct encoding - in notepad ++ for example you can change the encoding of the file.
  • put a UTF-8 meta tag on the page: <meta charset="UTF-8">

Generally only setting on the page is sufficient.

    
19.08.2014 / 03:36
6

Well, try the following code below to solve your problem.

Enter this code in your php, where you have your connection to the database.

<?php
    //O tipo de caracteres a ser usado
    header('Content-Type: text/html; charset=utf-8');

   //Depois da tua conexão a base de dados insere o seguinte código abaixo.
   //Esta parte vai resolver o teu problema!
    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');
?>

Enter this line in your html as shown in the example below

<meta http-equiv="content-type" content="text/html;charset=utf-8" />

Example in html

<!DOCTYPE html>
<html lang="pt">
    <head>
            <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <!--Introduza esta linha no teu html-->
            <title>Exemplo de Página</title>
    </head>
    <body>

    </body>
</html>
    
19.08.2014 / 12:28
5

Maybe it helps someone, I was having trouble with AJAX.

I was using it in php:

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

I've changed to:

*header('Content-type: text/html; charset=iso-8859-1');*
    
17.07.2017 / 10:42
4

In addition to everything that has already been answered, make sure your webserver is configured to use UTF-8 as an encoding.

In apache, add the following line to your configuration file:

AddDefaultCharset utf-8

In summary, you need to ensure four configurations:

1- Charset of php.ini (can be forced via ini_set('default_charset', 'UTF-8') )

2- Database charset (database collation utf8_* )

3- Meta-tag in HTML output

<meta http-equiv="content-type" content="text/html; charset=utf-8" />

HTML 5:

<meta charset="utf-8">

The webserver / php tries to guess from the file encoding, so it is interesting to always keep the files as UTF-8 without BOM ( Byte Order Mark ). UTF-8 with BOM generates errors with PHP (premature delivery of Header)

4- Webserver configuration (can be forced via header('Content-Type: text/html; charset=utf-8'); )

    
19.08.2014 / 12:55
1

depends on the coding you are using in each step, all have to be configured with the same encoding pattern ....

If what Marta said does not solve, try to use utf_encode, sometimes the encode switch to decode generates the ?? in accented words ...

    
19.08.2014 / 04:45