Question

I have an html file encoded in ANSI. if I try to browse it, I get non-understandable chars like question marks. I tried to put this tag:

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

but it didn't change any thing.

I converted the file into utf-8 and tried again and voila, it worked.

so I deleted the line above: <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> and opened the file and it's displayed ok.

So, my question, is: What does this line of code mean?

No correct solution

OTHER TIPS

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  • http-equiv attribute provides an HTTP header for the information/value of the content attribute.
  • The content attribute gives the value associated with the http-equiv or name attribute.
  • The charset attribute specifies the character encoding for the HTML document.

Effectively, the above <meta> declaration will instruct the browser to have text/html type of document with the character set set to UTF-8.

Including the meta declaration won't make much of a difference if the Content-Type header is already served over HTTP. That is, the real HTTP header takes precedence over everything (UTF BOM is an exception) except user override. The charset attribute is just meant as a fallback and will only be used if the document decoding using the charset specified in the HTTP header fails.

Note that this is pointless if the file is not saved as UTF-8. The charset will be effective only if the file is saved as UTF-8. To save it as UTF-8, you can simply add a Byte Order Mark (BOM), at the very beginning of the file:

$contents = file_get_contents('yourFile.ext');
file_put_contents($your_file, "\xEF\xBB\xBF".$content); 

See also:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top