Why does the php string sometimes return instead of some accented letters? [duplicate]

2

I asked a question about coding problem and json_encode of PHP.

In order not to generate a very broad question, I decided to ask this question separately.

Why does PHP sometimes return the character in the middle of a string containing accented characters?

Example:

My name Wallace

The strange thing is that sometimes the same letter can be replaced with , and it seems in some places of the printed string.

Example:

Meu nome � Wallace e estou com fé terei minhas dúvidas resolvidas

Note that é appears in the word , but it alone does not look like it.

Why does this happen?

What generates the character ?

    
asked by anonymous 09.10.2015 / 14:43

3 answers

1

This is because of the character set that your web page is set up on, which should be diverging from another set. Currently we have the ISO-8859-1 and UTF-8 character sets as the most used, and in PHP it is always recommended to use UTF-8 in the coding of your scripts.

Give to change the character set using the following command in PHP:

<?php
//Sempre coloque esse comando no início do seu script, depois da tag de abertura dele.
header('Content-Type: text/html; charset=utf-8');

Or also in the html page, using a meta tag, like this:

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

In html 5 you can use it this way:

<meta charset="utf-8">

On the html page it would look like this:

<!doctype html>
<html>
    <head>
        <title>Seu título da página</title>
        <meta http-equiv="content-type" content="text/html;charset=utf-8" />
        <!- Ou assim em html 5 -->
        <meta charset="utf-8">
    </head>
    <body>
        Conteúdo
    </body>
</html>
    
09.10.2015 / 15:01
1

To avoid this use everything in the same character set, preferably UTF-8.

When I say everything I want to say

  • The Encoding of .php, .js, .css, .html files and the ones with the most text.
  • The HTML header in the META tags
  • Database Encoding

Eventually it may happen that you have to work with more than one encoding because of different sources such as databases, files like EXCEL worksheets (which only work well with ISO-8859-1), etc.

For these cases use display functions like this

function toUTF8($string)
{
    if (function_exists('mb_detect_encoding')) {
        $current_encoding = mb_detect_encoding($string, 'UTF-8, ASCII, ISO-8859-1');
        $string = mb_convert_encoding($string, 'UTF-8', $current_encoding);         
    } else {
        $string =  utf8_decode(utf8_encode($string)) == $string ? utf8_encode($string) : $string;           
    }
    return $string;
}

function toLatin1($string)
{
    if (function_exists('mb_detect_encoding')) {
        $current_encoding = mb_detect_encoding($string, 'UTF-8, ASCII, ISO-8859-1');
        $string = mb_convert_encoding($string, 'ISO-8859-1', $current_encoding);            
    } else {
        $string = utf8_encode(utf8_decode($string)) == $string ? utf8_decode($string): $string;     
    }
    return $string;
}

In some situations, even these functions do not work. It is the case of strings concatenated with more than one encoding (believe it, this is not so unusual) and for such cases the conversion must be done character by character.

    
09.10.2015 / 19:27
0

This mainly occurs in data return of your database, as its contents can be as iso and uft8 data, or vice versa.

Try using the following function to convert the data to utf8:

  

echo mb_convert_encoding ($ variable, "UTF-8", "auto");

Remembering that you should add the following goal in your html:

  

< meta charset="UTF-8" >

    
09.10.2015 / 15:06