I’m busy trying to process the following RSS feed: Yahoo Search RSS, using the following code once the data is fetched:
$response = simplexml_load_string($data);
However, 99% of the chinese characters and strings disappear when I interrogate the simple xml object.
I’ve tried converting the incoming data to utf8 by doing:
$data = iconv("UTF-8", "UTF-8//TRANSLIT", $data);
But this also doesn’t help.
Before the data hits simplexml_load_string its 100% fine. But afterwards, its not.
Any ideas?
What you describe sounds like an encoding issue. Encoding is like a chain, if it get’s broken at one part of the processing, the data can be damaged.
When you request the data from the RSS server, you will get the data in a specific character encoding. The first thing you should find out is the encoding of that data.
According to the website headers, the encoding is UTF-8. This is the standard XML encoding.
However if the data is not UTF-8 encoded while the headers are saying so, you need to find out the correct encoding of the data and bring it into UTF-8 before you go on.
Next thing to check is if simplexml_load_string() is able to deal with UTF-8 data.
I do not use simplexml, I use DomDocument. So I can not say if or not. However I can suggest you to use DomDocument instead. It definitely supports UTF-8 for loading and all data it returns is encoded in UTF-8 as well. You should safely assume that simplexml handles UTF-8 properly as well however.
Next part of the chain is your display. You write that your data is broken. How can you say so? How do you interrogate the simplexml object?
Revisiting the Encoding Chain
As written, encoding is like a chain. If one element breaks, the overall result is damaged. To find out where it breaks, each element has to be checked on it’s own. The encoding you aim for is UTF-8 here.
<?xml version="1.0" encoding="UTF-8" ?>.var_dump()of the simple_xml object instance with the xml data suggests that it does not support CDATA. CDATA is used in the data in question. CDATA elements will get dropped.At this point this looks like the error you are facing. However you can convert all CDATA elements into text. To do this, you need to specify an option when loading the XML data. The option is a constant called
LIBXML_NOCDATAand it will merge CDATA as text nodes.The following is an example code I used for the tests above and demonstrates how to use the option:
I assume that this will fix your issue. If not DomDocument is able to handle CDATA elements. As the encoding chain is not further tested, you might still get encoding issues in the further processing of the data, so take care that you keep the encoding up to the output.