POST php com charset utf-8

Asked

Viewed 2,420 times

0

Good morning, after searching extensively for solutions to my problem, trying various solutions that have been suggested in other questions, like this one for example:

https://stackoverflow.com/questions/22653119/what-does-phps-mb-internal-encoding-actually-do

I am seeking information from a php post of a query to the Oracle database, but the output of this information may contain special characters: in the database is saved as "John" in the post comes out "Jo? the".

I would like the friends tip for a configuration (ideal that is global php.ini) where you can configure the output php charset. Some settings I’ve made:

PHP.ini

default_charset = "utf-8"
internal_encoding = 'utf-8'
output_encoding = 'utf-8'

Index.php

    <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
    <meta charset="utf-8" />
   </head>
    <body>
     <?php
      var_dump($result1);
     ?>

...["NAME"]=> string(4) "Jo? o" ...

Being that in the database is saved correctly:

banco

Connection to the bank:

$host="10.0.0.2";
$service="//10.0.0.2:1521/orcl";
$conn= new \PDO("oci:host=$host;dbname=$service","USER","senha");

If I give the command below to check the encoding of the string, I get 'ASCII'

echo mb_detect_encoding($result1['NOME']);

I have also tried using Function utf8_encode(); but nothing changes.. :(

If I write other texts in index.php that contain special characters they are displayed normally, so I believe it is not some config in the file at the editor or meta tag level.

Thank you very much in advance.

  • 1

    I’m pretty sure it has something to do with this: http://answall.com/questions/8429/php-echo-problema-specialcharacteristics-%C3%A7

  • @Miguel I tried to do the suggested in the post: setlocale(LC_ALL,'pt_BR.UTF8'); mb_internal_encoding('UTF8'); mb_regex_encoding('UTF8'); but then I get the error in the console: Uncaught Error: Call to Undefined f unction mb_internal_encoding()

  • Could you add your connection to the bank to the question? You have already taken a look at this topic: http://answall.com/questions/43193/d%C3%Bavida-com-charset-iso-8859-1-e-utf8

  • Can you see where it was recorded? Terminals or program files that can have this encoding?

  • @Allanandrade added the question.

  • @Williamasimiliar Andean made the data Insert in the database via normal INSERT.

  • @Gabrielhn, try this in your connection string => $Conn= new PDO("oci:host=$host;dbname=$service;charset=utf8","USER","password");

  • In my connection I added this line putenv("NLS_LANG=PORTUGUESE_BRAZIL.AL32UTF8") or die("Failed to insert the environment variable");

  • This might help http://answall.com/a/43205/3635

  • There is also the file Find. Notepad++ is the easiest editor to view Find the right file and set UTF8.

Show 5 more comments

2 answers

1


In your connection code was not informed the charset of connection.

Set the connection charset by adding ;charset=utf8 at the end of the connection string, example:

$conn= new \PDO("oci:host=$host;dbname=$service;charset=utf8","USER","s‌​enha");

Check the charset of tables and fields in your database:

All are with UTF-8?

Set header in PHP:

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

Set header in HTML (you’ve already done):

<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<meta charset="utf-8" />
  • Alan, I tried this way, but then when loading the page, I get the following error: Uncaught Exception 'Pdoexception' with message 'SQLSTATE[HY000]: Ocinlscharsetnametoid: Unknown Character set name (ext pdo_oci o ci_driver.c:610)' in C: www Intranet profile.php:12 When checking this link, I saw that UTF8 would be supported yes. Then I didn’t understand anything else: http://docs.oracle.com/cd/B19306_01/server.102/b14225/applocaledata.htm#i635016

0

I use some ways, in case one doesn’t work the other try rsrs

  • If that’s just the case John for Jo? the can try so when writing:

echo utf8_decode($nome);
  • The other way I use it is like this when it comes from the bank:

echo mb_strtoupper($nome,'UTF8');
  • Another is when I connect with the bank:

class ConnectionFactory{
    private $ora_user = "login"; 
    private $ora_senha = "senha"; 
    private $ora_bd = "(DESCRIPTION=
                        (ADDRESS_LIST=
                        (ADDRESS=(PROTOCOL=TCP)(HOST=ip-do-server)(PORT=1521))
                        )
                        (CONNECT_DATA=
                        (SERVICE_NAME=servico)
                        )
                        )"; 
    public  function  getConnection(){
            putenv("NLS_LANG=PORTUGUESE_BRAZIL.AL32UTF8") or die("Falha ao inserir a variavel de ambiente");
            $ora_conexao = oci_connect($this->ora_user, $this->ora_senha, $this->ora_bd);
        return $ora_conexao;

    }

    public function closeConnection($connection){
        $ora_conexao = oci_close($connection);

    }


}
  • Or as colleague Allan Andrade said, using the header:

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

It worked for me. I hope it helps

Browser other questions tagged

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