How to convert UTF-8 characters in PHP

Asked

Viewed 57,963 times

11

In my web application, I am using a Mysql database that has the table EditableContent. This table has the field content, who’s kind text and has as input format utf8-bin.

In the database the characters appear normally, but when fetching them and displaying them via method echo, accented characters are replaced by . By using the method uf8_decode($meu_texto), the same characters are replaced by ?.

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 contents were copied to the Notepad++ database, and the file formatting was as Codificação em UTF-8(Sem BOM)

6 answers

16


Let’s see..

What colleagues @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 you use the <meta ../> or the function header() of PHP. If you’re a host on a shared VM, even if you’re paid (cheap/free host is guaranteed to be so), you’ll have to ask the server support to change for you - I’ve had this problem. Try this if nothing below works.

1 - Through the tag in HTML

In HTML 5 default, you can use

<meta charset="utf-8" />

In the previous patterns, you can use

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

2 - Through PHP

You can set up the pure page in PHP and call, right at the beginning, the function

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

3 - Check the configuration of the database tables

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

4 - Pure PHP page without header()

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

Note: Valid for redirects as well.

8

Some things can help, for example

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

Usually just setting on the page is enough.

7

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

Enter this code in your php, where has its connection to 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');
?>

Insert this line into 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>
  • How do you know if it will solve the problem? It might be using PDO or mysqli. Ideal is to set it up in mysql

  • You’re right at that point but starting with his question doesn’t look like he’s using PDO... It’s also an option, you can set it up in Mysql itself.

  • Regardless of the driver used, this is the only response that mentions that the connection to Mysql also needs to be configured as UTF-8. There is still some really complete answer to the question, why don’t you complement your showing everything that needs to be in UTF-8?

6

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

3

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

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

AddDefaultCharset utf-8

In short, you need to ensure four settings:

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

2- Database charset (collation of database utf8_*)

3-Metatag in HTML output

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

HTML 5:

<meta charset="utf-8">

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

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

0

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

if what Marta said does not solve, tries to use utf_encode, sometimes the exchange of Ncode to Code generates the ?? in accented words...

Browser other questions tagged

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