Displaying HTML Entities in Magpie RSS
by Stuart BownessWe use the Magpie RSS reader to display the list of ‘Recent Journal Entries’ on the front page. It does its job well, but it sometimes fails to display HTML entities like apostrophes and quotes correctly. Instead, they appear as a ‘?’. This problem comes up with some frequency on the Magpie mailing list, but I wasn’t able to find a working solution there.
As it turns out, Magpie doesn’t automatically detect the character encoding of the RSS it reads. You have to manually define the character encoding of both the input RSS and the output XHTML. Since we use UTF-8, adding these two lines of PHP before including Magpie <? include(’rss_fetch.inc’) ?> did the trick:
define('MAGPIE_INPUT_ENCODING', 'UTF-8');
define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
If that doesn’t work, you need to check the character encoding yourself. The input encoding is defined RSS file, at the top, in a line like this: <?xml version="1.0" encoding="UTF-8"?>. The output encoding is that of your XHTML page, usually defined in the Content-Type <meta> tag.



This fix didn’t work for me. What did, was defining the output encoding before loading Magpie, as follows:
define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
require('magpierss/rss_fetch.inc');
Mapgie then spits out UTF-8 encoded characters, which can be converted to HTML entities quite easily, e.g.:
$output = htmlentities($output, ENT_QUOTES, "UTF-8");
This solution doesn’t require hacking the Magpie source.
Matthew
Thanks Matthew! Your ‘define’ solution worked brilliantly for me (although I had to change the curly single quotes in your comment to straight single quotes, but I’m sure that was a result of the commenting system here and not your intention).
Matthew – thanks for your post you htmlentities call was exactly what I needed…..
We have updated our page based on Matthew’s tip with a few modifications. Hope it helps!
Thank you very much for posting this…I believe you’ve just saved me a TON of time.
Excellent solution – worked perfectly, thanks for saving me the headache.
Sweet, I was looking for exactly this solution! Thank you!
sweet and easy fix–thanks Nathan and Matthew!
Thanks man… it really did the job!
Thanks. Exactly what i needed … after i realized, that magpierss was killing my german umlauts
Great work! Thanks!
thanks for the quick fix!
This worked for me too… Excellent!
Thank you!
great thanks, worked for me
My thanks to Nathan & Matthew. Precisely what I was looking for.
Hallo guys i think i have the same proble i am using magpierss under pligg. and in some imports of rss i cant see correctly the same html entities. for instanse i cant see
Erm, where to put these two lines!????!?!?!?!
Before you include the magpie rss in your include syntax.
Great article, this addresses the problem perfectly. Simple, concise, well done.
Thank you.
thnx for soln