Convert string to utf-8
I am needing to transform a string into UTF-8 encoding. I'm declaring it like this:
$nome = utf8_encode($nome);
And still I can't find the error.
The string appears like this:
Consolação
3 answers
If you are having problems in the browser, you are probably using the wrong encode.
Add the header <meta charset="utf-8" />
to the head
section of your page, as below:
<html lang="pt-br">
<head>
<meta charset="utf-8" />
Preferably put it at the beginning, preferably before the <title>
tag, which can usually be changed by the user, as in a search. Security failures can occur in older browsers if this is not followed (source ).
Apparently the conversion worked or was done twice. What can happen is that the page is being shown in another encoding. Try putting
<meta charset="UTF-8" />
On the <head>
page and see if it appears correctly.
Alternatively, you can use htmlentities()
for display, and not depend on the encoding for the screen.
$nome_para_display_na_tela = htmlentities($nome);
Remembering that in any case, you will always need the right encoding for the database.
I use a combination of things, for example in php I put
header("Content-Type: text/html; charset=UTF-8",true);
And in html inside head tags
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">