2013-03-19 69 views
2

我正在使用SimplePie(v1.3.1)在网页上显示RSS提要。其中一个标题包含欧元(€)符号,并在页面上显示为“”。 当我在他们的网站上使用SimplePie的演示它正确显示,所以它应该没有问题。但是我无法让它工作。PHP - 特殊字符(欧元符号)显示不正确(RSS提要/ SimplePie)

我已经做了:

  • 搜索上了SimplePie的网站和文档
  • 搜索上计算器的网站在谷歌
  • 搜索
  • 试了几十给出的“解决方案”中没有任何的运气

我能找到的是,它可能是一个字符编码问题,据我所知这s应该设置为UTF-8。 Beneith是我目前的测试代码,基于SimplePie演示。我已经通过SimplePie的FAQ添加了3个解决方案(请参阅I'm seeing weird characters)。

我在做什么错?

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

[编辑] 更多信息:

  • 从RSS的XML具有UTF-8编码(<?xml version="1.0" encoding="UTF-8"?>
  • 使用echo str_replace("€", "&euro;", $item->get_title());作品,但不是很好
+0

只要我没有更好的答案,我使用Gareth的字符串替换的想法。虽然这很有效(这里并不令人意外),但我真的希望有一个更稳固的解决方案。 – 4ice 2013-03-19 15:58:28

回答

0

用特殊字符实体替换符号。

&euro; 

如果让:€

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

+0

RSS源来自另一个页面,所以我无法在此进行任何更改。 – 4ice 2013-03-19 10:24:46

+0

那么这是没有好处的O.o运行一个字符串替换从RSS提取的数据。解决这个问题最有可能是一种更简单的方法,但我无法将它想到我的头顶。 – 2013-03-19 10:25:44

+0

一个字符串替换可能会工作,但这将是我最后的手段,因为它不是一个“干净的”解决方案。希望别人有其他想法。无论如何,谢谢你的意见。 – 4ice 2013-03-19 10:36:33

0

至于我能看到你的问题是张贴在饲料中的含量是不是UTF-8,即使你的网页是UTF-8 。

所有你需要做的就是用这个

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

希望能够解决你的问题

+0

不幸的不是。这不会改变任何东西。 – 4ice 2013-03-19 11:00:51

+0

[链接](http://stackoverflow.com/questions/6559822/how-to-convert-any-character-encoding-to-utf8-on-php)可能会帮助你 – cooldude5757 2013-03-19 11:16:35

+0

尝试给你的链接给出的可能性,但不改变结果。 RSS feed XML的编码设置为 <?xml version =“1.0”encoding =“UTF-8”?> – 4ice 2013-03-19 11:29:19

3

我也有当欧元符号被显示为“â,¬”与编码摆弄后的问题一段时间没有成功,我只是跟着以下字符串替换:

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

http://www.mauserrifle.nl/php/php-and-replacing-euro-signs/所示。因为这是一个后端工作,所以我没有任何性能损失的问题。