Using UTF-8 and libXML

As you can guess from the title, this is not a really a Yii based article.  Recently, I spent a few frustrating hours trying to get some data loaded through libXML using simplexml_load_string with various odd parsing errors like:

Entity: line 21: parser error : EntityRef: expecting ';' in 

It's not UTF-8

As a European, I use UTF-8 all the time.  The database is UTF-8.  The Db connection is UTF-8.  My data is validated as UTF-8.  Do you follow?

But, and this is a big BUT ...

libXML doesn't always like it.

The Solution

As often, was really simple, to re-validate and pass everything through utf8_encode before passing it to simplexml_load_string or simplexml_load_file

$c=utf8_encode (file_get_contents($url));

Did you know you can hire me?

I take on projects of all sizes. From Consulting to large Development Projects.

If you're starting a new Yii project and would like some help to get setup and running or you need some help with a particular module or you just need someone to develop the whole dang thing, then just ask ...


Leave a Comment

twitterfacebookgooglelinkedin https://me.yahoo.com