13

I have a page whose encoding is declared with

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

But when I enter the page, another encoding (ISO) is chosen in the browser. I have tried to set the encoding by a PHP method

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

But it also didn't help. All source files are encoded in UTF-8 without a BOM. The only solution, which I tried, and it had worked, was setting the encoding in the .htaccess file by adding the AddDefaultCharset UTF-8 line, but then other pages on the server were not displayed correctly. How can I solve this problem?

11
  • 4
    Please note that the meta content type tag is ignored when the page is served over HTTP. It's only used when you let the browser save the page to disk and then open it from the disk by file:///. The charset as specified in charset attribute of the HTTP response Content-Type header is been used when the page is served over HTTP. Perhaps you're setting the header too late in PHP (it has to be set before you write any bit to the response, you would however have been notified of this mistake if you have set the proper PHP error reporting level). Commented Oct 18, 2011 at 15:42
  • How are you able to identify the encoding is different? Are characters not displaying appropriately? Is it SQL DB fetched text, in which case mysql_set_charset can help... Commented Oct 18, 2011 at 15:49
  • 2
    @Ergo: that wouldn't have been solved by setting AddDefaultCharset UTF-8 in .htaccess. Commented Oct 18, 2011 at 15:49
  • @BalusC: the php header was sent as first bits, nothing before it was sent. I know that wrong encoding is chosen beacause I can see it in Firefox: screen (Sorry for non-English screen, chosen menu items says: developer tools -> characters set -> unicode). In my page ISO-8859-2 is set by default, but when I click UTF-8 all special characters are displayed correctly. Commented Oct 18, 2011 at 16:01
  • 1
    @BalusC: <meta> is used to determine the character encoding of a page, even if the page is served over HTTP. However, the Content-Type header takes precedence over the <meta> tag, if it is present. See w3.org/TR/html401/charset.html#h-5.2.2 Commented Oct 19, 2011 at 7:09

1 Answer 1

8

Disable the default charset:

AddDefaultCharset Off
Sign up to request clarification or add additional context in comments.

5 Comments

I reckon, that that will cause that other pages on server which have to be encoded in ISO will not be displayed correctly. (Similarly to AddDefaultCharset UTF-8.)
If apache doesn't send the charset in the headers, the browsers will guess the encoding. Most likely they will guess iso-8859-2.
You are right, it helped. I hope it is valid and in accordance with strandards.
Don't worry about it being invalid. It is the default value of apache. Somehow your server was configured differently. So you are good now.
Where? What? How?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.