Displaying HTML Entities in Magpie RSS


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

15 Responses to “Displaying HTML Entities in Magpie RSS”

  1. Matthew Carroll Says:

    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. Luis Mauricio Says:

    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

Leave a Reply

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-Spam Image