2012-08-13 21 views
0

我正在尝试使用SimplePie解析客户端的RSS源(客户端是华盛顿邮报的作者)。SimplePie 1.3字符编码问题

阅读完文档并使用示例代码作为参考之后,我能够将提要解析到网站中,但现在我遇到了撇号字符未解码的问题('显示作为')

我试图在FAQ了SimplePie使用建议的解决方案来解决这个问题:1. 验证,该网站的meta标签 2.使用了SimplePie的handle_content_type()函数 3.使用PHP的内置标题()函数来纠正HTTP标头

不幸的是没有一个e已经为我解决了这个问题。

下面是我使用来解析RSS提要的代码:

<?php 

require_once('php/autoloader.php'); 

$feedJB = new SimplePie(); 
$feedJB->set_feed_url('http://washingtontimes.dynamic.feedsportal.com/pf/637323/communities.washingtontimes.com/neighborhood/feeds/latest/status-update/'); 
$feedJB->init(); 
$feedJB->handle_content_type(); 

$feedRB = new SimplePie(); 
$feedRB->set_feed_url('http://washingtontimes.dynamic.feedsportal.com/pf/637323/communities.washingtontimes.com/neighborhood/feeds/latest/2nd-golden-era-advertising/'); 
$feedRB->init(); 
$feedRB->handle_content_type(); 

?> 

这是页面上的输出代码:我已经在我的机器本地测试这个

<!-- Left --> 
      <li class="left"> 
       <h3>Recent Posts</h3> 
       <ul class="feed-list"> 
        <?php foreach ($feedJB->get_items(0, 5) as $item): ?> 
        <li> 
         <strong><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title(); ?></a></strong> 
         <small>Posted on <?php echo $item->get_date('j F Y'); ?></small> 
        </li> 
        <?php endforeach; ?> 
        <li><h4><a href="<?php echo $feedJB->get_permalink(); ?>">Read more articles by Jeff</a></h4></li> 
       </ul> 
      </li> 
      <!-- /Left --> 

      <!-- Right --> 
      <li class="right"> 
       <h3>Recent Posts</h3> 
       <ul class="feed-list"> 
        <?php foreach ($feedRB->get_items(0, 5) as $item): ?> 
        <li> 
         <strong><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title(); ?></a></strong> 
         <small>Posted on <?php echo $item->get_date('j F Y'); ?></small> 
        </li> 
        <?php endforeach; ?> 
        <li><h4><a href="<?php echo $feedRB->get_permalink(); ?>">Read more articles by Rob</a></h4></li> 
       </ul> 
      </li> 
      <!-- /Right --> 

(运行MAMP的Mac Pro Lion)以及我的网络服务器(Linux运行Apache 2.2.22 & PHP 5.2.17)。

您还可以查看本作的时间去以下链接为: http://clients.josephmainwaring.com/statuscreative/#!columns.php

如果任何人有意见,以解决字符编码问题,将不胜感激。

+0

尝试更换使用$文本与右单引号(’或’)撇号(')= str_replace函数( “'”,“””,$文字); (代码仅用于清晰) – Waygood 2012-08-13 16:29:13

+0

@ Waygood - 感谢您的建议,但它没有工作... – theaccordance 2012-08-13 19:38:17

回答

0

我发现华盛顿邮报的提要都是作为ISO-8859-1提供的,即使它们包含UTF-8字符。我不使用了SimplePie,但我每次取饲料的时候,我通过下面的函数,其中$xml是饲料的文本运行它,$url是饲料的网址:

function feed_fix_broken ($xml, $url) { 
    $xml = iconv('UTF-8', 'UTF-8//IGNORE', $xml); 
    $broken = array ('washingtonpost.com' => 'ISO-8859-1'); 
    foreach ($broken as $domain => $encoding) { 
    if (stristr($url, $domain)) { 
     $xml = iconv('UTF-8', $encoding.'//TRANSLIT', $xml); 
    } 
    } 
    return $xml; 
} 

这个音译UTF- 8编码实体与其ISO-8859-1对应实体。

注意,在FeedDemon的, “查韦斯” 是扭曲......

"Chávez" is screwy...

,但我是正确的。

but I've got it right