user1063295
user1063295

Reputation:

Change browser encoding with PHP?

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.

Upvotes: 1

Views: 623

Answers (4)

Tyil
Tyil

Reputation: 1807

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().

Upvotes: 1

Quentin
Quentin

Reputation: 943143

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.

Upvotes: 3

GolezTrol
GolezTrol

Reputation: 116100

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.

Upvotes: 2

RageZ
RageZ

Reputation: 27313

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

Upvotes: 2

Related Questions