File or character encoding error

Asked

Viewed 1,871 times

3

On every page of a site I’m developing with coding UTF-8, defined both in the charset of <head> how much in the editor encoding, I’m having problem in the rendering of words that have accents, for example:

The word Medic render as M dico.

Considering I’m testing in my localhost, I use PHP and the problem persists in all browsers I tested, how can I solve this problem?

  • 2

    Post the code of the page with problem, but can be in the configuration of the server or even the browser.

  • I am testing localhost. The problem is the same with all browsers. inside <head> usage: <meta charset="utf-8">

  • PHP? If you use header('content-type: text/html; charset=utf-8');

  • PHP yes. I already use as mentioned above.

  • Probably your code editor is in another encoding, or your DB.

  • 2

    You did not mention PHP in the question. If you are using a database, it is also "relevant" to mention. And this question about charset depends on many parameters. The problem may have N causes.

Show 1 more comment

3 answers

3


I usually do it this way:

  • In PHP code, use the top of the Index code header('Content-Type: text/html; charset=utf-8');
  • In HTML, put <meta charset="UTF-8">
  • In your editor, crop all content, change the file encoding to (UTF-8 without BOM) and then paste the content again.
  • If you are using PDO together with Mysql, you can use this parameter in building your connection: PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
  • Take the custom of always creating UTF-8 files without GOOD from the start - See here

This should solve your problem in 99% of cases.

2

The problem may be related to both the file encoding and some error in the output of your PHP.

Start by changing/checking the file encoding by opening it in your IDE, changing the option to encoding (or Portuguese coding), for UTF-8. Below are the step by step two editors that I believe are the most used by current professionals in the market and dev. beginners:

if you use the Sublime text you can change going into:

File -> reopen with enconding -> UTF-8 (English)

File -> reopen with encoding -> UTF-8 (English)

if you use the Notepad:

Encoding -> Convert to UTF-8 (English)

Encoding -> Convert to UTF-8 (English)

For other IDE’s you can consult the documentation of the same.

After done the above step, check if in PHP, you are using exactly header('Content-Type: text/html; charset=utf-8');.

Also check using a debug (such as the Dev Tools of Google) if the output of your code is exactly this: <meta charset="UTF-8">, without any loving character. Some time ago I read a text about this code that contains the charset need to be among the first 512kb of the document to avoid some bugs in some browsers. Unfortunately I could not find the link for you to take a look and I also believe that the problem related to this should already be solved, however, just to ensure that recommendation between the 1st and 3rd line of your file, I usually put right after the <meta http-equiv="X-UA-Compatible" content="IE=Edge"> being in the first line after the opening of the <head>.

Well, if after all this your problem persists, it can also be a GOOD (byte-order mark) being related to ASCII, usually caused by copies by copy and Paste also known as CTRL+V and CTRL+C, I talked about this in that reply. Simply put, to resolve you can open the file in a basic editor (for example the windows text editor) that does not interpret Unicode or one that interprets but is able to display non-ASCII characters and delete the problem code snippet by writing the same.

Finally, it may also be related to connection Mysql, if used, according to that answer user’s Jpsy in the OS. You can solve by assigning PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8" on the PDO constructor call.

1

Open the file in Notepad++ and change your encoding (coding): Encoding -> Convert to UTF-8 or Codificação -> Converter para UTF-8. If you do not want to (or cannot) download Notepad++, this should be done in your IDE.

Browser other questions tagged

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