Displaying HTML Entities in Magpie RSS

by Stuart Bowness

We 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.

21 Responses to “Displaying HTML Entities in Magpie RSS”

  1. 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

  2. Tim Houghton says:

    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).

  3. Todd Kempf says:

    Matthew – thanks for your post you htmlentities call was exactly what I needed…..

  4. Stuart says:

    We have updated our page based on Matthew’s tip with a few modifications. Hope it helps!

  5. Ian says:

    Thank you very much for posting this…I believe you’ve just saved me a TON of time.

  6. Chris Kelley says:

    Excellent solution – worked perfectly, thanks for saving me the headache.

  7. Konrad Burman says:

    Sweet, I was looking for exactly this solution! Thank you!

  8. sweet and easy fix–thanks Nathan and Matthew!

  9. Cliff says:

    Thanks man… it really did the job!

  10. Elmar says:

    Thanks. Exactly what i needed … after i realized, that magpierss was killing my german umlauts

  11. Hein says:

    Great work! Thanks!

  12. dev says:

    thanks for the quick fix!

  13. Esther says:

    This worked for me too… Excellent!

  14. Arnau says:

    Thank you! :)

  15. Simon says:

    great thanks, worked for me

  16. Abed says:

    My thanks to Nathan & Matthew. Precisely what I was looking for.

  17. john karalis says:

    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

  18. name req says:

    Erm, where to put these two lines!????!?!?!?!

  19. Before you include the magpie rss in your include syntax.

  20. Great article, this addresses the problem perfectly. Simple, concise, well done.

    Thank you.

  21. manish says:

    thnx for soln

Trackbacks/Pingbacks

Leave a Reply

*
Type the security word shown at the right, below.
Click to hear an audio file of the anti-spam word