为什么我的编码错误?

Why do I have wrong encoding?

我正在尝试创建 rss 提要。我使用 yii 框架。

这是我的部分观点

<?php
header("Content-Type: application/xml; charset=utf-8");
?>
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">

    <channel>
        <title>Newskeeper</title>
        <link>newskeeper.ru</link>
        <description>news</description>

        <?php
        $HOST = "http://newskeeper.ru/";
        $HOST_NEWS = "news#news_";
        $IMAGE_PATH = $HOST . "news/sendshow?id=";

        foreach($news as $item) {
            $date=date("D, j M Y G:i:s", $item['create_time']). " GMT";

            echo '<item>';
                echo '<title>' . utf8_encode(html_entity_decode($item['teaser_ru']))  . '</title>';
                echo '<link>' . utf8_encode(html_entity_decode($HOST .$HOST_NEWS . $item['slug'])) . '</link>';
                echo '<description>' .utf8_encode(html_entity_decode($item['text_ru'])). '</description>';
            echo '</item>';
        }
        ?>

    </channel>
</rss>

这是控制器中的方法class

public function actionRss() {
    $sql = "select id, teaser_ru, text_ru, slug, create_time from news order by create_time desc limit 10";
    $command = Yii::app()->db->createCommand($sql);
    $news = $command->queryAll();
    $this->renderPartial("_rss", array('news' => $news));
}

问题是为什么不显示俄文字母。它以错误的编码显示它们。

我发现了问题。我过度编码了。您需要删除 utf8_encode 并将第二个和第三个参数添加到 html_entity_decode,因此 rss 提要的项目应如下所示

echo '<item>';
     echo '<title>' . html_entity_decode($item['teaser_ru'], ENT_COMPAT, 'utf-8')  . '</title>';
     echo '<link>' . html_entity_decode($HOST .$HOST_NEWS . $item['slug'], ENT_COMPAT, 'utf-8') . '</link>';
     echo '<description>' . html_entity_decode($item['text_ru'], ENT_COMPAT, 'utf-8') . '</description>';
echo '</item>';