2

I am using SimplePie (v1.3.1) to display a RSS feed on a webpage. One of the titles contains a euro (€) sign and it is displayed as € on the page. When I use the SimplePie's demo on their website it is displayed correctly, so it should be no problem. However I can't get it to work.

What I already did:

  • Search on SimplePie's website and documentation
  • Search on stackoverflow's website
  • Search on Google
  • Tried dozens of given "solutions" without any luck

What I can find is that it is probably a character encoding problem and as far as I know this should be set to UTF-8. Beneith is my current test code, based on a SimplePie demo. I already added the 3 given solutions by SimplePie's FAQ (see I'm seeing weird characters).

What am I doing wrong?

<?php
header('Content-type:text/html; charset=utf-8');
// Make sure SimplePie is included. You may need to change this to match the location of autoloader.php
// For 1.3+:
require_once('./php/autoloader.php');

// We'll process this feed with all of the default options.
$feed = new SimplePie();

// Set the feed to process.
$feed->set_feed_url(***RSS_FEED_URL_HERE***);

// Run SimplePie.
$feed->init();

// This makes sure that the content is sent to the browser as text/html and the UTF-8 character set (since we didn't change it).
$feed->handle_content_type();

// Let's begin our XHTML webpage code.  The DOCTYPE is supposed to be the very first thing, so we'll keep it on the same line as the closing-PHP tag.
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
    <title>Sample SimplePie Page</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <style type="text/css">
    /*some css*/
    </style>
</head>
<body>
    <div class="header">
        <h1><a href="<?php echo $feed->get_permalink(); ?>"><?php echo $feed->get_title(); ?></a></h1>
        <p><?php echo $feed->get_description(); ?></p>
    </div>
    <?php
    foreach ($feed->get_items() as $item):
    ?>
        <div class="item">
            <h2><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title(); ?></a></h2>
            <p><?php echo $item->get_description(); ?></p>
            <p><small>Posted on <?php echo $item->get_date('j F Y | g:i a'); ?></small></p>
        </div>
    <?php endforeach; ?>
</body>
</html>

[EDIT] More info:

  • The XML from the RSS has UTF-8 encoding (<?xml version="1.0" encoding="UTF-8"?>)
  • Using echo str_replace("€", "&euro;", $item->get_title()); works, but is not very nice
4ice
  • 29
  • 1
  • 7
  • As long as I do not have a better answer I used Gareth's idea of a string replace. Although this works (no surprise here), I really hope for a more solid solution. – 4ice Mar 19 '13 at 15:58

3 Answers3

4

I also had the problem where the euro sign was shown as "€ " After fiddling with the encoding for a while without success, I just went with following string replacement:

str_replace(chr(0xE2).chr(0x82).chr(0xAC), '&euro;', $mystring)

As shown on http://www.mauserrifle.nl/php/php-and-replacing-euro-signs/ . Since it was a backend job, I had no problem with the performance loss.

Jenever
  • 500
  • 5
  • 17
0

Replace the symbol with the special character entity.

&euro;

if makes: €

http://www.utexas.edu/learn/html/spchar.html

  • The RSS feed is from another page, so I am not able to make any changes here. – 4ice Mar 19 '13 at 10:24
  • Well that's of no benefit O.o run a string replace on the data pulled from the RSS. There is most likely an easier way of solving this but I can't think of it off the top of my head. – Gareth Preston Mar 19 '13 at 10:25
  • A string replace would probably work, but it would be my last resort since it's not a "clean" solution. Hopefully someone else has some other ideas. Anyway, thanks for you input. – 4ice Mar 19 '13 at 10:36
  • Replacing original content is never clean, but you're welcome I'm glad that in someway you have taken my input into mind. :) – Gareth Preston Mar 19 '13 at 10:41
0

As far as I can see your problem is the content posted by the feed is not in UTF-8 even though your page is in UTF-8.

All you have to do is use this

<?php
utf8_encode ($feed )// the rss feed from another page
?>

Hope that solves your issue

  • Unfotunately not. This does not change anything. – 4ice Mar 19 '13 at 11:00
  • [link](http://stackoverflow.com/questions/6559822/how-to-convert-any-character-encoding-to-utf8-on-php) might help you – cooldude5757 Mar 19 '13 at 11:16
  • Tried the given possibilities on your link, but non change the outcome. The encoding of the RSS feed XML is set to – 4ice Mar 19 '13 at 11:29
  • Well then there is only one thing you can do : **String Replace** – cooldude5757 Mar 19 '13 at 11:31
  • I doubt that, like I said: the online demo on SimplePie is working, so it should be possible to fix (I don't think that SimplePie is using a string replace). – 4ice Mar 19 '13 at 11:34