Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UTF-8 encoding xml in PHP

Tags:

php

xml

utf-8

I'm trying to output XML using PHP, and when I look at the page source in Firefox everything seems fine. However, the page itself doesn't display properly.

In Firefox, it usually says this at the top of the page when it's showing correctly formatted XML:

This XML file does not appear to have any style information associated with it. The document tree is shown below.

..and then it shows the xml tree.

However, I just get the characters within the xml tags, and no tree.

The only difference I can see is that my page is encoded as Western ISO-8859-1 and the correctly displayed XML is encoded as Unicode UTF-8. So how would I output my page as Unicode UTF8?

I've tried this but it doesn't make any difference:

echo utf8_encode($xmlstring);
like image 862
cannyboy Avatar asked Nov 12 '10 12:11

cannyboy


1 Answers

Make sure you send the XML with an appropriate content-type and encoding, e.g.

<?php header("Content-Type: application/xml; charset=utf-8"); ?>

Also check that the XML prolog contains the proper encoding, e.g.

<?xml version="1.0" encoding="UTF-8"?>

The message about the style information refers to a missing processing instruction, e.g.

<?xml-stylesheet type="text/xsl" href="someting"?>

which would tell the browser how to style/format the output. It is not necessarily needed if you just want to display the raw XML.

like image 129
Gordon Avatar answered Oct 19 '22 06:10

Gordon