PHP - 特殊字符(欧元符号)未正确显示(RSS feed/SimplePie)

标签 php character-encoding rss simplepie

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

我已经做了什么:

  • 搜索 SimplePie 的网站和文档
  • 在 stackoverflow 网站上搜索
  • 在 Google 上搜索
  • 尝试了数十种给定的“解决方案”,但没有任何运气

我发现这可能是一个字符编码问题,据我所知应该设置为UTF-8。 Beneith 是我当前的测试代码,基于 SimplePie 演示。我已经添加了 SimplePie 常见问题解答中给出的 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());可以,但不是很好

最佳答案

我还遇到了欧元符号显示为“â”的问题,在摆弄编码一段时间但没有成功后,我只是进行了以下字符串替换:

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

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

关于PHP - 特殊字符(欧元符号)未正确显示(RSS feed/SimplePie),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15496719/

相关文章:

php - 如何使用 Php 管理不同 MySql 表中的多个价目表和项目?

php - mysql utf8字符问题

c# - SQL Server 上的编码问题

swift - 如何解决 ios swift 中的 XMLParser.ErrorCode.invalidCharacterError ?

flash - Blogger Atom/RSS 提要中的 YouTube 视频

php - MySQL 外键..困惑

php - 使用 PHP 控制用户输入的 Javascript/HTML/textarea 并防止 iframe hack

javascript - 特殊字符 '\u0098' 使用 charCodeAt() 读取为 '\u02dc'

java - RSS阅读器然后抓取页面内容

php - 图片大小调整php代码