1

I've got a program on which I have non-ASCII characters which do not show properly on ISO-8859-1. Is there a way to use PHP and change the browser encoding somehow, and also allow the characters to display properly in the browser even though the encoding is ISO-8859-1?

Much Appreciated.

1
  • So if it's not ISO-8859-1, what is the charset? Commented Dec 21, 2011 at 13:28

4 Answers 4

3

Use the header function to send an (explicit) HTTP Content-Type response header.

header('Content-Type: text/html; charset=ISO-8859-1');

… replacing ISO-8859-1 with whatever encoding you are actually using. Hopefully that will be UTF-8.

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

Comments

2

you should use the header function

header( 'Content-Type: text/html; charset=ISO-8859-1');

Note: you should make sure no content have been sent to the browser or you can't modify the headers anymore, so I advise you to use this code as soon as possible in your script

Comments

2

The browser itself doesn't have an encoding. It supports many encodings and uses the one you tell it too. If you specify (in headers and/or HTML) that the encoding is ISO-8859-1, then your document should be in that encoding and you should make sure that all characters you send are in the right encoding. So you should actually send ISO-8859-1 characters. You cannot send a document that uses different encodings for different sections of the document.

For some characters, you may post an HTML entity instead. For instance é can be sent as é. This will work, regardless of encoding.

If you have the choice, I'd opt to use UTF-8. It supports any character and you don't have to worry about escaping diacritics or other special characters, except those that are special to HTML/XML itself.

Comments

1

Like others have said, using the header function:

header('Content-type: text/html; charset=ISO-8859-1');

or, if you want to serve valid XHTML files instead of the standard HTML:

header('Content-type: application/xml+xhtml; charset=ISO-8859-1');

It is possible to call the header later on in the script, unlike what RageZ said, but you will need to have enabled output buffering for that, using ob_start().

3 Comments

The registered MIME type for XHTML is application/xml+xhtml there is no text/xhtml.
You have a syntax error in both examples as you fail to terminate the string in either.
Fixed the XHTML content-type, and the strings are being terminated properly. Sorry about that :P

Your Answer

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