Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set HTTP header to UTF-8 using PHP which is valid in W3C validator

I have several PHP pages echoing out various things into HTML pages with the following code.

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

However, when I validate using the W3C validator it comes up with:

The character encoding specified in the HTTP header (iso-8859-1) is different from the value in the element (utf-8).

I am quite new to PHP, and I was wondering if I could and should change the header for the PHP files to match the HTML files.

like image 472
manycheese Avatar asked Nov 25 '10 16:11

manycheese


People also ask

How do I set character encoding in HTTP header?

Use the header() function before generating any content, e.g.: header('Content-type: text/html; charset=utf-8'); Java Servlets.

Does PHP support UTF-8?

PHP does not natively support UTF-8. This is fairly important to keep in mind when dealing with UTF-8 encoded data in PHP.

What encoding do HTTP headers use?

HTTP messages are encoded with ISO-8859-1 (which can be nominally considered as an enhanced ASCII version, containing umlauts, diacritic and other characters of West European languages). At the same time, the message body can use another encoding assigned in "Content-Type" header.


1 Answers

Use header to modify the HTTP header:

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

Note to call this function before any output has been sent to the client. Otherwise the header has been sent too and you obviously can’t change it any more. You can check that with headers_sent. See the manual page of header for more information.

like image 154
Gumbo Avatar answered Sep 27 '22 23:09

Gumbo