views:

911

answers:

2

I have a PHP script that reads in data from an XML file, returns it via AJAX to a page which then places the data in to the relevant text area.

The Content-Type of the page is as follows:

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

The XML heading looks like this:

<?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE xsl:stylesheet  [
    <!ENTITY pound  "&#163;">
 ]>
...

The problem is that when the data arrives in the text box it has the character  before it.

I was under the impression that setting the content type to UTF-8 fixes this problem but I must be wrong. Can anyone tell me which encoding type I need to use to get it to render consistently?

+6  A: 

The problem with character encoding is if some program or process, at any step, fails to deal with it correctly then characters can be munged.

So, you have

  1. An HTML page that's
  2. making an AJAX call which
  3. runs some code on the server that
  4. builds an XML response froman XML file that is
  5. returned to the browser and displayed in a text box

So, ideally, at every step, you want UTF-8 encoding. It's likely at some step in the process your character is getting munged. Here's the steps I'd take to track this down

  1. Make sure that your HTML/PHP page has the correct Meta Tag
  2. Make sure that your HTML/PHP text file is saved as UTF-8
  3. Make sure that your XML file that's read from has been saved as UTF-8
  4. Make sure that your XML file that's read from and the response you build both have their encoding set in the prolouge <?xml version="1.0" encoding="utf-8"?>
  5. Make sure that the response headers of your server are sending out utf-8
  6. Call your service manually in a browser to see if the symbol is being returned correctly
Alan Storm
+3  A: 

You need to ensure that your AJAX request is returning UTF-8 as well. Setting the Content-Type of the static page is fine, but it won't affect the Content-Type encoding of the HTTP transfer that takes place when you make the Ajax call.

Try using this code in the script that returns the Ajax results (before any other output). This will set the Content-Type of your HTTP response:

header("Content-Type: text/html; charset=utf8\n");
zombat
Cheers, not sure if I have this but I think my php.ini is set to serve pages as UTF-8 by default anyway :)
xenon