How to set HTTP header to UTF-8 using PHP which is valid in W3C validator
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.
PHP & HTML5: UTF-8 document declaration with <meta> tag or through the header() function?
I could not describe it better than: The Road to HTML 5: character encoding
it's a 7-step algorithm; step 4 has 2 sub-steps, the first of which has 7 branches, one of which has 8 sub-steps, one of which actually links to a separate algorithm that itself has 7 steps... It goes on like that for a while. The gist of it is
- User override. - You have no influence on this
An HTTP "charset" parameter in a "Content-Type" field. In PHP code that is:
header('Content-Type: text/html;charset=UTF-8');
A Byte Order Mark before any other data in the HTML document itself. - I can not suggest to actually make use of that feature. If you like, just save your files accordingly, but do not expect the
header()
calls working flawlessly any longer. The alternative is to output the BOM manually, in PHP that is:echo "\xEF\xBB\xBF"; # UTF-8 BOM
But even then I can not recommend to output a BOM because this is an backwards incompatible change for the output. These guidelines are for reading - not outputting.
A META declaration with a "charset" attribute. - Please do so, this is good practice. In HTML 5 that is:
<meta charset="UTF-8">
A META declaration with an "http-equiv" attribute set to "Content-Type" and a value set for "charset". - Why not?! In HTML 5 that would be:
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
- Unspecified heuristic analysis. - You have no influence on this.
Those are the points. My recommendation are as following:
- Check your webserver is sending correct headers when serving the HTML.
- Have your HTML as well those meta-tags so that it's possible to save the HTML file on disk and open it later in a browser (offline, archive).
- Do not put BOM inside the document if you're using UTF-8.
- Do not use UTF-16 or UTF-32, if you use Unicode, use UTF-8.
If you are targetting systems that are totally unaware to encodings, use US-ASCII and mask everything else not part of it as HTML entities.
Note: This entitites suggestion is for output to the browser and not for storing, storing is something that falls in your area, ensure you are aware about encodings when you handle your store. Never use HTML entities for example when you write HTML into your mysql database when you don't really need it (e.g.
&
in HTML links).
Decoding UTF-8 Encoded Header
Here's what you're doing wrong: You're HTML (as generated by the PHP) is not UTF-8 encoded. So even though it's returning the accented c, the page isn't displaying it correctly.
To fix it, add this in your <head>
tag:
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
How to set UTF-8 encoding for a PHP file
header('Content-type: text/plain; charset=utf-8');
How do I set character encoding to UTF-8 for default.html?
You need to replace the HTTP-level header.
This should work:
<?php
header('Content-type: text/html; charset=utf-8');
?>
Note that the above must be the first thing in your file. No exceptions. See header.
For general information on how to change the character set header in different web stacks, see Setting the HTTP charset parameter.
Ajax, Request Header UTF-8 to ISO Charset
I'm afraid that AJAX POST requests must use UTF-8. The jQuery manual explains it:
POST data will always be transmitted to the server using UTF-8 charset, per the W3C XMLHTTPRequest standard.
You might now wonder about the contentType
setting:
Note: The W3C XMLHttpRequest specification dictates that the charset is always UTF-8; specifying another charset will not force the browser to change the encoding.
In other words, you have no choice. You either need to migrate your server-side code to UTF-8, make an explicit conversion —iconv()
and mb_convert_encoding()
will come in handy— or figure out a witty JavaScript trick (such as serialising data before submission).
Special character PHP HTML
It's important that your entire line code has the same charset to avoid issues where characters displays incorrectly.
There are quite a few settings that needs to be properly defined and I'd strongly recommend UTF-8, as this has most letters you would need (Scandinavian, Greek, Arabic).
Here's a little list of things that has to be set to a specific charset.
Headers
Setting the charset in both HTML and PHP headers to UTF-8
PHP:
header('Content-Type: text/html; charset=utf-8');
(PHP headers has to be placed before any kind output (echo, whitespace, HTML))HTML:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
(HTML-headers are placed within the<head>
/</head>
tag)
File-encoding
It might also be needed for the file itself to be UTF-8 encoded. If you're using Notepad++ to write your code, this can be done in the "Format" drop-down on the taskbar. You should use UTF-8 w/o BOM
(see this SO).
Other
- Some specific functions have the attribute of a specific charset, and if you are using such functions, it should be specified there as well. Read the documentation for each function.
Should you follow all of the pointers above, chances are your problem will be solved. If not, you can take a look at this StackOverflow post: UTF-8 all the way through.
PHP header for xml with utf-8
The header()
function just modifies HTTP headers. The code you posted sets a Content-Type
header, which is important for telling browsers and other clients what type of file you're serving.
The <?xml version="1.0"?>
line you're talking about is part of the document itself, and not affected by the HTTP headers.
Your tags say you're using DOM to create your XML document. If you change your DomDocument constructor to also pass the charset,
$doc = new DomDocument('1.0', 'UTF-8');
It should output that charset in the XML document.
Related Topics
How to Resolve Ambiguous Column Names When Retrieving Results
PHP Echo VS PHP Short Echo Tags
Parse Youtube Video Id Using Preg_Match
Getting Title and Meta Tags from External Website
Make Xampp/Apache Serve File Outside of Htdocs Folder
PHP - Accessing Multidimensional Array Values
How to Use a JavaScript Variable as a PHP Variable
How to Use Order by For Multiple Columns in Laravel 4
Base_Url() Function Not Working in Codeigniter
Split String by New Line Characters
Reference: Comparing PHP'S Print and Echo
Global or Singleton For Database Connection
How to Find the PHP.Ini File Used by the Command Line